Mercurial > retraceit
comparison src/metadataview.cpp @ 35:f10d4e035eec
(issue10) Use width of header as minimum of the column size
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 06 May 2015 16:08:41 +0200 |
parents | 4e16fbd10945 |
children | 0c05958d254c |
comparison
equal
deleted
inserted
replaced
34:40a683d1a318 | 35:f10d4e035eec |
---|---|
18 #include <QDebug> | 18 #include <QDebug> |
19 #include <QModelIndex> | 19 #include <QModelIndex> |
20 #include <QHeaderView> | 20 #include <QHeaderView> |
21 #include <QItemSelectionModel> | 21 #include <QItemSelectionModel> |
22 #include <QSettings> | 22 #include <QSettings> |
23 #include <QFontMetrics> | |
24 #include <QApplication> | |
23 | 25 |
24 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : | 26 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : |
25 QWidget(parent, f) { | 27 QWidget(parent, f) { |
26 /* Create models */ | 28 /* Create models */ |
27 mSortModel = new QSortFilterProxyModel; | 29 mSortModel = new QSortFilterProxyModel; |
45 | 47 |
46 mView = new QTableView; | 48 mView = new QTableView; |
47 mView->setModel(mSortModel); | 49 mView->setModel(mSortModel); |
48 | 50 |
49 mView->horizontalHeader()->setStretchLastSection(true); | 51 mView->horizontalHeader()->setStretchLastSection(true); |
50 mView->resizeColumnsToContents(); | |
51 // mView->setColumnWidth(0, 60); | 52 // mView->setColumnWidth(0, 60); |
52 mView->setSelectionBehavior(QAbstractItemView::SelectRows); | 53 mView->setSelectionBehavior(QAbstractItemView::SelectRows); |
53 mView->setSelectionMode(QAbstractItemView::SingleSelection); | 54 mView->setSelectionMode(QAbstractItemView::SingleSelection); |
54 mView->setSortingEnabled(true); | 55 mView->setSortingEnabled(true); |
55 mView->setEditTriggers(QAbstractItemView::NoEditTriggers); | 56 mView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
66 } | 67 } |
67 | 68 |
68 mSortModel->setSourceModel(mCSVModel); | 69 mSortModel->setSourceModel(mCSVModel); |
69 qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; | 70 qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; |
70 applyDefaultSort(); | 71 applyDefaultSort(); |
72 resizeColsToHeaders(); | |
71 return QString(); | 73 return QString(); |
72 } | 74 } |
73 | 75 |
74 void MetaDataView::dataChanged() | 76 void MetaDataView::dataChanged() |
75 { | 77 { |
177 idx = 1; | 179 idx = 1; |
178 break; | 180 break; |
179 } | 181 } |
180 } | 182 } |
181 if (idx == -1) { | 183 if (idx == -1) { |
182 qDebug() << "Failed to find configured sort column: " << sortField; | |
183 return; | 184 return; |
184 } | 185 } |
185 qDebug() << "Applying default sort order on column " << idx; | 186 qDebug() << "Applying default sort order on column " << idx; |
186 mView->sortByColumn(idx, sortAsc ? Qt::AscendingOrder : Qt::DescendingOrder); | 187 mView->sortByColumn(idx, sortAsc ? Qt::AscendingOrder : Qt::DescendingOrder); |
187 } | 188 } |
188 | 189 |
190 void MetaDataView::resizeColsToHeaders() { | |
191 QFontMetrics fm(qApp->font()); | |
192 /* We do this manually here to avoid resizing to the real contents as | |
193 * we want the columns in the width of the header data. And we only | |
194 * want to increase that size. */ | |
195 for (int i=0; i < mSortModel->columnCount(); i++) { | |
196 const QString entry = mSortModel->headerData(i, Qt::Horizontal).toString(); | |
197 int w = fm.width(entry) + 20; | |
198 if (w > mView->horizontalHeader()->sectionSize(i)) { | |
199 mView->horizontalHeader()->resizeSection(i, w); | |
200 qDebug() << "Resizing " << i << " to: " << w; | |
201 } | |
202 } | |
203 } |