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());} QColor bannerTextColor(){return stringToColor(settings.value("Options/BannerTextColor","222,222,222").toString());}
int clipboardTimeOut(){return settings.value("Options/ClipboardTimeOut",20).toInt();} int clipboardTimeOut(){return settings.value("Options/ClipboardTimeOut",20).toInt();}
QBitArray columns(){return stringToBitArray(settings.value("UI/Columns","11111000000").toString(),11);} 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);} 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();} int columnSort(){return settings.value("UI/ColumnSort",0).toInt();}
Qt::SortOrder columnSortOrder(){return static_cast<Qt::SortOrder>(settings.value("UI/ColumnSortOrder",Qt::AscendingOrder).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);} QBitArray searchColumns(){return stringToBitArray(settings.value("UI/SearchColumns","11110000001").toString(),11);}
QList<int> searchColumnOrder(){return stringToIntArray(settings.value("UI/SearchColumnOrder","100,100,100,100,100,100,100,100,100,100,100").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);} 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();} int searchColumnSort(){return settings.value("UI/SearchColumnSort",0).toInt();}
Qt::SortOrder searchColumnSortOrder(){return static_cast<Qt::SortOrder>(settings.value("UI/SearchColumnSortOrder",Qt::AscendingOrder).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) { KeepassEntryView::KeepassEntryView(QWidget* parent) : QTreeWidget(parent) {
ViewMode=Normal; ViewMode=Normal;
AutoResizeColumns = true;
header()->setResizeMode(QHeaderView::Interactive); header()->setResizeMode(QHeaderView::Interactive);
header()->setStretchLastSection(false); header()->setStretchLastSection(false);
header()->setClickable(true); header()->setClickable(true);
header()->setCascadingSectionResizes(true); header()->setCascadingSectionResizes(true);
header()->setStretchLastSection(true); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
retranslateColumns(); retranslateColumns();
restoreHeaderView(); restoreHeaderView();
connect(header(), SIGNAL(sectionResized(int,int,int)), SLOT(resizeColumns()));
connect(this,SIGNAL(itemSelectionChanged()), SLOT(OnItemsChanged())); connect(this,SIGNAL(itemSelectionChanged()), SLOT(OnItemsChanged()));
connect(&ClipboardTimer, SIGNAL(timeout()), SLOT(OnClipboardTimeOut())); connect(&ClipboardTimer, SIGNAL(timeout()), SLOT(OnClipboardTimeOut()));
connect(this, SIGNAL(itemActivated(QTreeWidgetItem*,int)), SLOT(OnEntryActivated(QTreeWidgetItem*,int))); 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) { void KeepassEntryView::setColumnVisible(int col, bool visible) {
if (columnVisible(col) == visible)
return; // nothing to do
header()->setSectionHidden(col, !visible); header()->setSectionHidden(col, !visible);
if (visible)
header()->resizeSection(col, columnSizes[col]);
} }
void KeepassEntryView::saveHeaderView() { void KeepassEntryView::saveHeaderView() {
QBitArray columns(NUM_COLUMNS); QBitArray columns(NUM_COLUMNS);
QList<int> columnOrder; QList<int> columnOrder;
QList<int> columnsSizes;
int columnSort = header()->sortIndicatorSection(); int columnSort = header()->sortIndicatorSection();
Qt::SortOrder columnSortOrder = header()->sortIndicatorOrder(); Qt::SortOrder columnSortOrder = header()->sortIndicatorOrder();
for (int i=0; i<NUM_COLUMNS; ++i) { for (int i=0; i<NUM_COLUMNS; ++i) {
columns.setBit(i, columnVisible(i)); columns.setBit(i, columnVisible(i));
columnOrder << header()->visualIndex(i); columnOrder << header()->visualIndex(i);
columnsSizes << header()->sectionSize(i);
} }
if (ViewMode == Normal) { if (ViewMode == Normal) {
config->setColumns(columns); config->setColumns(columns);
config->setColumnOrder(columnOrder); config->setColumnOrder(columnOrder);
config->setColumnSizes(columnsSizes); config->setColumnSizes(columnSizes);
config->setColumnSort(columnSort); config->setColumnSort(columnSort);
config->setColumnSortOrder(columnSortOrder); config->setColumnSortOrder(columnSortOrder);
} }
else { else {
config->setSearchColumns(columns); config->setSearchColumns(columns);
config->setSearchColumnOrder(columnOrder); config->setSearchColumnOrder(columnOrder);
config->setSearchColumnSizes(columnsSizes); config->setSearchColumnSizes(columnSizes);
config->setSearchColumnSort(columnSort); config->setSearchColumnSort(columnSort);
config->setSearchColumnSortOrder(columnSortOrder); config->setSearchColumnSortOrder(columnSortOrder);
} }
} }
void KeepassEntryView::restoreHeaderView() { void KeepassEntryView::restoreHeaderView() {
AutoResizeColumns = false;
QBitArray columns; QBitArray columns;
QList<int> columnOrder; QList<int> columnOrder;
QList<int> columnSizes;
int columnSort; int columnSort;
Qt::SortOrder columnSortOrder; Qt::SortOrder columnSortOrder;
@ -123,19 +129,65 @@ void KeepassEntryView::restoreHeaderView() {
columnSortOrder = config->searchColumnSortOrder(); 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 QMap<int,int> order; // key=visual index; value=logical index
for (int i=0; i<NUM_COLUMNS; ++i) { for (int i=0; i<NUM_COLUMNS; ++i) {
if (columnOrder[i] == 100)
columnOrder[i] = ++lastVisibleIndex;
order.insert(columnOrder[i], i); order.insert(columnOrder[i], i);
setColumnVisible(i, false); // initally hide all columns 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) { for (QMap<int,int>::const_iterator i = order.constBegin(); i != order.constEnd(); ++i) {
header()->moveSection(header()->visualIndex(i.value()), NUM_COLUMNS-1); 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())); setColumnVisible(i.value(), columns.testBit(i.value()));
} }
header()->setSortIndicator(columnSort, columnSortOrder); 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){ void KeepassEntryView::OnGroupChanged(IGroupHandle* group){
@ -426,8 +478,8 @@ void KeepassEntryView::contextMenuEvent(QContextMenuEvent* e){
} }
void KeepassEntryView::resizeEvent(QResizeEvent* e){ void KeepassEntryView::resizeEvent(QResizeEvent* e){
// TODO resizeColumns(); resizeColumns();
e->accept(); QTreeWidget::resizeEvent(e);
} }

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