2 * alarmlistview.cpp - widget showing list of alarms
4 * Copyright © 2007,2008,2010 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "alarmlistview.h"
24 #include <ksharedconfig.h>
25 #include <kconfiggroup.h>
27 #include <QHeaderView>
28 #include <QApplication>
31 AlarmListView::AlarmListView(const QByteArray
& configGroup
, QWidget
* parent
)
32 : EventListView(parent
),
33 mConfigGroup(configGroup
)
35 setEditOnSingleClick(true);
36 connect(header(), &QHeaderView::sectionMoved
, this, &AlarmListView::sectionMoved
);
39 void AlarmListView::setModel(QAbstractItemModel
* model
)
41 EventListView::setModel(model
);
42 KConfigGroup
config(KSharedConfig::openConfig(), mConfigGroup
.constData());
43 QByteArray settings
= config
.readEntry("ListHead", QByteArray());
44 if (!settings
.isEmpty())
45 header()->restoreState(settings
);
46 header()->setMovable(true);
47 header()->setStretchLastSection(false);
48 header()->setResizeMode(AlarmListModel::TimeColumn
, QHeaderView::ResizeToContents
);
49 header()->setResizeMode(AlarmListModel::TimeToColumn
, QHeaderView::ResizeToContents
);
50 header()->setResizeMode(AlarmListModel::RepeatColumn
, QHeaderView::ResizeToContents
);
51 header()->setResizeMode(AlarmListModel::ColourColumn
, QHeaderView::Fixed
);
52 header()->setResizeMode(AlarmListModel::TypeColumn
, QHeaderView::Fixed
);
53 header()->setResizeMode(AlarmListModel::TextColumn
, QHeaderView::Stretch
);
54 header()->setStretchLastSection(true); // necessary to ensure ResizeToContents columns do resize to contents!
55 const int margin
= QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin
);
56 header()->resizeSection(AlarmListModel::ColourColumn
, viewOptions().fontMetrics
.lineSpacing() * 3 / 4);
57 header()->resizeSection(AlarmListModel::TypeColumn
, AlarmListModel::iconWidth() + 2*margin
+ 2);
60 /******************************************************************************
61 * Called when the column order is changed.
62 * Save the new order for restoration on program restart.
64 void AlarmListView::sectionMoved()
66 KConfigGroup
config(KSharedConfig::openConfig(), mConfigGroup
.constData());
67 config
.writeEntry("ListHead", header()->saveState());
71 /******************************************************************************
72 * Set which time columns are to be displayed.
74 void AlarmListView::selectTimeColumns(bool time
, bool timeTo
)
77 return; // always show at least one time column
78 // bool changed = false;
79 bool hidden
= header()->isSectionHidden(AlarmListModel::TimeColumn
);
82 // Unhide the time column
83 header()->setSectionHidden(AlarmListModel::TimeColumn
, false);
86 else if (!time
&& !hidden
)
88 // Hide the time column
89 header()->setSectionHidden(AlarmListModel::TimeColumn
, true);
92 hidden
= header()->isSectionHidden(AlarmListModel::TimeToColumn
);
95 // Unhide the time-to-alarm column
96 header()->setSectionHidden(AlarmListModel::TimeToColumn
, false);
99 else if (!timeTo
&& !hidden
)
101 // Hide the time-to-alarm column
102 header()->setSectionHidden(AlarmListModel::TimeToColumn
, true);
107 // resizeLastColumn();
108 // triggerUpdate(); // ensure scroll bar appears if needed
113 void AlarmListView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
115 for (int col = topLeft.column(); col < bottomRight.column(); ++col)
117 if (col != header()->resizeMode(col) == QHeaderView::ResizeToContents)
118 resizeColumnToContents(col);