Restore old column auto-resize behaviour

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@328 b624d157-de02-0410-bad0-e51aec6abb33
master
sniperbeamer 16 years ago
parent 6e4936c630
commit 29ded0a62b
  1. 6
      src/KpxConfig.h
  2. 70
      src/lib/EntryView.cpp
  3. 3
      src/lib/EntryView.h

@ -50,12 +50,12 @@ public:
QColor bannerTextColor(){return stringToColor(settings.value("Options/BannerTextColor","222,222,222").toString());}
int clipboardTimeOut(){return settings.value("Options/ClipboardTimeOut",20).toInt();}
QBitArray columns(){return stringToBitArray(settings.value("UI/Columns","11111000000").toString(),11);}
QList<int> columnOrder(){return stringToIntArray(settings.value("UI/ColumnOrder","100,100,100,100,100,100,100,100,100,100,100").toString(),11);}
QList<int> columnOrder(){return stringToIntArray(settings.value("UI/ColumnOrder","1,2,3,4,5,6,7,8,9,10,11").toString(),11);}
QList<int> columnSizes(){return stringToIntArray(settings.value("UI/ColumnSizes","15,10,10,10,10,10,10,10,10,10,10").toString(),11);}
int columnSort(){return settings.value("UI/ColumnSort",0).toInt();}
Qt::SortOrder columnSortOrder(){return static_cast<Qt::SortOrder>(settings.value("UI/ColumnSortOrder",Qt::AscendingOrder).toInt());}
QBitArray searchColumns(){return stringToBitArray(settings.value("UI/SearchColumns","11111000000").toString(),11);}
QList<int> searchColumnOrder(){return stringToIntArray(settings.value("UI/SearchColumnOrder","100,100,100,100,100,100,100,100,100,100,100").toString(),11);}
QBitArray searchColumns(){return stringToBitArray(settings.value("UI/SearchColumns","11110000001").toString(),11);}
QList<int> searchColumnOrder(){return stringToIntArray(settings.value("UI/SearchColumnOrder","2,3,4,5,6,7,8,9,10,11,1").toString(),11);}
QList<int> searchColumnSizes(){return stringToIntArray(settings.value("UI/SearchColumnSizes","15,10,10,10,10,10,10,10,10,10,10").toString(),11);}
int searchColumnSort(){return settings.value("UI/SearchColumnSort",0).toInt();}
Qt::SortOrder searchColumnSortOrder(){return static_cast<Qt::SortOrder>(settings.value("UI/SearchColumnSortOrder",Qt::AscendingOrder).toInt());}

@ -34,14 +34,16 @@ KeepassEntryView* pEntryView;*/
KeepassEntryView::KeepassEntryView(QWidget* parent) : QTreeWidget(parent) {
ViewMode=Normal;
AutoResizeColumns = true;
header()->setResizeMode(QHeaderView::Interactive);
header()->setStretchLastSection(false);
header()->setClickable(true);
header()->setCascadingSectionResizes(true);
header()->setStretchLastSection(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
retranslateColumns();
restoreHeaderView();
connect(header(), SIGNAL(sectionResized(int,int,int)), SLOT(resizeColumns()));
connect(this,SIGNAL(itemSelectionChanged()), SLOT(OnItemsChanged()));
connect(&ClipboardTimer, SIGNAL(timeout()), SLOT(OnClipboardTimeOut()));
connect(this, SIGNAL(itemActivated(QTreeWidgetItem*,int)), SLOT(OnEntryActivated(QTreeWidgetItem*,int)));
@ -68,42 +70,46 @@ bool KeepassEntryView::columnVisible(int col) {
}
void KeepassEntryView::setColumnVisible(int col, bool visible) {
if (columnVisible(col) == visible)
return; // nothing to do
header()->setSectionHidden(col, !visible);
if (visible)
header()->resizeSection(col, columnSizes[col]);
}
void KeepassEntryView::saveHeaderView() {
QBitArray columns(NUM_COLUMNS);
QList<int> columnOrder;
QList<int> columnsSizes;
int columnSort = header()->sortIndicatorSection();
Qt::SortOrder columnSortOrder = header()->sortIndicatorOrder();
for (int i=0; i<NUM_COLUMNS; ++i) {
columns.setBit(i, columnVisible(i));
columnOrder << header()->visualIndex(i);
columnsSizes << header()->sectionSize(i);
}
if (ViewMode == Normal) {
config->setColumns(columns);
config->setColumnOrder(columnOrder);
config->setColumnSizes(columnsSizes);
config->setColumnSizes(columnSizes);
config->setColumnSort(columnSort);
config->setColumnSortOrder(columnSortOrder);
}
else {
config->setSearchColumns(columns);
config->setSearchColumnOrder(columnOrder);
config->setSearchColumnSizes(columnsSizes);
config->setSearchColumnSizes(columnSizes);
config->setSearchColumnSort(columnSort);
config->setSearchColumnSortOrder(columnSortOrder);
}
}
void KeepassEntryView::restoreHeaderView() {
AutoResizeColumns = false;
QBitArray columns;
QList<int> columnOrder;
QList<int> columnSizes;
int columnSort;
Qt::SortOrder columnSortOrder;
@ -123,19 +129,65 @@ void KeepassEntryView::restoreHeaderView() {
columnSortOrder = config->searchColumnSortOrder();
}
// compatibility with KeePassX <= 0.4.0 (100 = column hidden)
int lastVisibleIndex = -1;
for (int i=0; i<NUM_COLUMNS; ++i) {
if (columnOrder[i]!=100 && columnOrder[i]>lastVisibleIndex)
lastVisibleIndex = columnOrder[i];
}
QMap<int,int> order; // key=visual index; value=logical index
for (int i=0; i<NUM_COLUMNS; ++i) {
if (columnOrder[i] == 100)
columnOrder[i] = ++lastVisibleIndex;
order.insert(columnOrder[i], i);
setColumnVisible(i, false); // initally hide all columns
if (columnSizes[i] < header()->minimumSectionSize())
columnSizes[i] = header()->minimumSectionSize();
}
for (QMap<int,int>::const_iterator i = order.constBegin(); i != order.constEnd(); ++i) {
header()->moveSection(header()->visualIndex(i.value()), NUM_COLUMNS-1);
header()->resizeSection(i.value(), std::max(columnSizes[i.value()], header()->minimumSectionSize()));
header()->resizeSection(i.value(), columnSizes[i.value()]);
setColumnVisible(i.value(), columns.testBit(i.value()));
}
header()->setSortIndicator(columnSort, columnSortOrder);
AutoResizeColumns = true;
resizeColumns();
}
void KeepassEntryView::resizeColumns() {
if (!AutoResizeColumns)
return;
AutoResizeColumns = false;
int w = viewport()->width();
int sum = 0;
for (int i=0; i<NUM_COLUMNS; ++i) {
if (columnVisible(i))
sum += header()->sectionSize(i);
}
double stretch = (double)w / (double)sum;
for (int i=0; i<NUM_COLUMNS; ++i) {
if (columnVisible(i) && header()->sectionSize(i)!=0) {
int size = qRound(header()->sectionSize(i) * stretch);
header()->resizeSection(i, size);
columnSizes[i] = size;
}
else {
columnSizes[i] = qRound(columnSizes[i] * stretch);
}
}
AutoResizeColumns = true;
}
void KeepassEntryView::OnGroupChanged(IGroupHandle* group){
@ -426,8 +478,8 @@ void KeepassEntryView::contextMenuEvent(QContextMenuEvent* e){
}
void KeepassEntryView::resizeEvent(QResizeEvent* e){
// TODO resizeColumns();
e->accept();
resizeColumns();
QTreeWidget::resizeEvent(e);
}

@ -53,6 +53,8 @@ class KeepassEntryView:public QTreeWidget{
IGroupHandle* CurrentGroup;
enum EntryViewMode {Normal, ShowSearchResults};
EntryViewMode ViewMode;
bool AutoResizeColumns;
QList<int> columnSizes;
void updateEntry(EntryViewItem*);
void editEntry(EntryViewItem*);
@ -87,6 +89,7 @@ class KeepassEntryView:public QTreeWidget{
void removeDragItems();
void OnEditOpenUrl();
void OnEditCopyUrl();
void resizeColumns();
signals:
void fileModified();