Fix colum visibility selection in the divelist

Several changes:
- split the reload of the DiveListView from the reload of the header
- don't include the column title in the name of the setting; the title
  will change depending on the units and localization chosen by the user
- rename the slot that toggles visibility to make the code more readable
- use setCollumHidden() method to simplify the code
- don't save the width of hidden columns (as they would be saved as zero
  width and can then no longer be enabled)

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-05-26 10:12:45 -07:00
parent 2896dbdaf1
commit 43892e36fd
3 changed files with 19 additions and 17 deletions

View file

@ -40,6 +40,10 @@ void DiveListView::reload()
else else
setCurrentIndex(firstDiveOrTrip); setCurrentIndex(firstDiveOrTrip);
} }
}
void DiveListView::reloadHeaderActions()
{
// Populate the context menu of the headers that will show // Populate the context menu of the headers that will show
// the menu to show / hide columns. // the menu to show / hide columns.
if (!header()->actions().size()) { if (!header()->actions().size()) {
@ -48,24 +52,23 @@ void DiveListView::reload()
QSettings s; QSettings s;
s.beginGroup("DiveListColumnState"); s.beginGroup("DiveListColumnState");
for(int i = 0; i < model()->columnCount(); i++) { for(int i = 0; i < model()->columnCount(); i++) {
QString title = QString("show %1").arg(model()->headerData( i, Qt::Horizontal).toString()); QString title = QString("show %1").arg(model()->headerData(i, Qt::Horizontal).toString());
QString settingName = QString("showColumn%1").arg(i);
QAction *a = new QAction(title, header()); QAction *a = new QAction(title, header());
bool shown = s.value(settingName, true).toBool();
a->setCheckable(true); a->setCheckable(true);
a->setChecked( s.value(title, true).toBool()); a->setChecked(shown);
a->setProperty("index", i); a->setProperty("index", i);
connect(a, SIGNAL(triggered(bool)), this, SLOT(hideColumnByIndex())); a->setProperty("settingName", settingName);
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleColumnVisibilityByIndex()));
header()->addAction(a); header()->addAction(a);
if (a->isChecked()) setColumnHidden(i, !shown);
showColumn(true);
else
hideColumn(false);
} }
s.endGroup(); s.endGroup();
s.sync();
} }
} }
void DiveListView::hideColumnByIndex() void DiveListView::toggleColumnVisibilityByIndex()
{ {
QAction *action = qobject_cast<QAction*>(sender()); QAction *action = qobject_cast<QAction*>(sender());
if (!action) if (!action)
@ -73,14 +76,10 @@ void DiveListView::hideColumnByIndex()
QSettings s; QSettings s;
s.beginGroup("DiveListColumnState"); s.beginGroup("DiveListColumnState");
s.setValue(action->text(), action->isChecked()); s.setValue(action->property("settingName").toString(), action->isChecked());
s.endGroup(); s.endGroup();
s.sync(); s.sync();
setColumnHidden(action->property("index").toInt(), !action->isChecked());
if (action->isChecked())
showColumn(action->property("index").toInt());
else
hideColumn(action->property("index").toInt());
} }
void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command) void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command)

View file

@ -31,7 +31,8 @@ public:
void reload(); void reload();
public slots: public slots:
void hideColumnByIndex(); void toggleColumnVisibilityByIndex();
void reloadHeaderActions();
Q_SIGNALS: Q_SIGNALS:
void currentDiveChanged(int divenr); void currentDiveChanged(int divenr);

View file

@ -44,6 +44,7 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow())
ui->ProfileWidget->setFocusProxy(ui->ListWidget); ui->ProfileWidget->setFocusProxy(ui->ListWidget);
ui->ListWidget->reload(); ui->ListWidget->reload();
readSettings(); readSettings();
ui->ListWidget->reloadHeaderActions();
ui->ListWidget->setFocus(); ui->ListWidget->setFocus();
ui->globe->reload(); ui->globe->reload();
instance = this; instance = this;
@ -432,7 +433,8 @@ void MainWindow::writeSettings()
settings.beginGroup("ListWidget"); settings.beginGroup("ListWidget");
for (i = TreeItemDT::NR; i < TreeItemDT::COLUMNS; i++) for (i = TreeItemDT::NR; i < TreeItemDT::COLUMNS; i++)
settings.setValue(QString("colwidth%1").arg(i), ui->ListWidget->columnWidth(i)); if (!ui->ListWidget->isColumnHidden(i))
settings.setValue(QString("colwidth%1").arg(i), ui->ListWidget->columnWidth(i));
settings.endGroup(); settings.endGroup();
settings.beginGroup("Units"); settings.beginGroup("Units");
SAVE_VALUE("feet", units.length); SAVE_VALUE("feet", units.length);