1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
SelectTaskDialog.cpp
This file is part of Charm, a task-based time tracking application.
Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Frank Osterfeld <frank.osterfeld@kdab.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SelectTaskDialog.h"
#include "ExpandStatesHelper.h"
#include "GUIState.h"
#include "ViewHelpers.h"
#include <QPushButton>
#include <QSettings>
#include "ui_SelectTaskDialog.h"
SelectTaskDialogProxy::SelectTaskDialogProxy(CharmDataModel *model, QObject *parent)
: ViewFilter(model, parent)
{
// we filter for the task name column
setFilterKeyColumn(Column_TaskId);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setFilterRole(TasksViewRole_Filter);
prefilteringModeChanged();
}
bool SelectTaskDialogProxy::filterAcceptsColumn(int column, const QModelIndex &) const
{
return column == Column_TaskId;
}
Qt::ItemFlags SelectTaskDialogProxy::flags(const QModelIndex &index) const
{
if (index.isValid()) {
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
} else {
return Qt::NoItemFlags;
}
}
QVariant SelectTaskDialogProxy::data(const QModelIndex &index, int role) const
{
if (index.isValid() && role == Qt::CheckStateRole) {
return QVariant();
} else {
return ViewFilter::data(index, role);
}
}
SelectTaskDialog::SelectTaskDialog(QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::SelectTaskDialog())
, m_proxy(MODEL.charmDataModel())
{
m_ui->setupUi(this);
m_ui->treeView->setModel(&m_proxy);
m_ui->treeView->header()->hide();
m_ui->buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
connect(m_ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &SelectTaskDialog::slotCurrentItemChanged);
connect(m_ui->treeView, &QTreeView::doubleClicked,
this, &SelectTaskDialog::slotDoubleClicked);
connect(m_ui->filter, &QLineEdit::textChanged,
this, &SelectTaskDialog::slotFilterTextChanged);
connect(m_ui->showExpired, &QCheckBox::toggled,
this, &SelectTaskDialog::slotPrefilteringChanged);
connect(m_ui->showSelected, &QCheckBox::toggled,
this, &SelectTaskDialog::slotPrefilteringChanged);
connect(this, &SelectTaskDialog::accepted,
this, &SelectTaskDialog::slotAccepted);
connect(MODEL.charmDataModel(), &CharmDataModel::resetGUIState,
this, &SelectTaskDialog::slotResetState);
connect(m_ui->filter, &QLineEdit::textChanged,
this, &SelectTaskDialog::slotSelectTask);
QSettings settings;
settings.beginGroup(QString::fromUtf8(staticMetaObject.className()));
if (settings.contains(MetaKey_MainWindowGeometry))
resize(settings.value(MetaKey_MainWindowGeometry).toSize());
// initialize prefiltering
slotPrefilteringChanged();
m_ui->filter->setFocus();
}
SelectTaskDialog::~SelectTaskDialog()
{
}
void SelectTaskDialog::slotResetState()
{
QSettings settings;
settings.beginGroup(QString::fromUtf8(staticMetaObject.className()));
GUIState state;
state.loadFrom(settings);
QModelIndex index(m_proxy.indexForTaskId(state.selectedTask()));
if (index.isValid())
m_ui->treeView->setCurrentIndex(index);
Q_FOREACH (const TaskId id, state.expandedTasks()) {
QModelIndex indexForId(m_proxy.indexForTaskId(id));
if (indexForId.isValid())
m_ui->treeView->expand(indexForId);
}
m_ui->showExpired->setChecked(state.showExpired());
m_ui->showSelected->setChecked(state.showCurrents());
}
void SelectTaskDialog::showEvent(QShowEvent *event)
{
slotResetState();
QDialog::showEvent(event);
}
void SelectTaskDialog::hideEvent(QHideEvent *event)
{
QSettings settings;
settings.beginGroup(QString::fromUtf8(staticMetaObject.className()));
settings.setValue(MetaKey_MainWindowGeometry, size());
QDialog::hideEvent(event);
}
TaskId SelectTaskDialog::selectedTask() const
{
return m_selectedTask;
}
void SelectTaskDialog::selectTask(TaskId task)
{
m_selectedTask = task;
QModelIndex index(m_proxy.indexForTaskId(m_selectedTask));
if (index.isValid())
m_ui->treeView->setCurrentIndex(index);
else
m_ui->treeView->setCurrentIndex(QModelIndex());
}
void SelectTaskDialog::slotCurrentItemChanged(const QModelIndex &first, const QModelIndex &)
{
const Task task = m_proxy.taskForIndex(first);
if (isValidAndTrackable(first)) {
m_selectedTask = task.id();
m_ui->taskStatusLB->clear();
} else {
m_selectedTask = 0;
const bool expired = !task.isCurrentlyValid();
const bool trackable = task.trackable();
const bool notTrackableAndExpired = (!trackable && expired);
const QString expirationDate = QLocale::system().toString(
task.validUntil(), QLocale::ShortFormat);
const QString info = notTrackableAndExpired ? tr(
"The selected task is not trackable and expired since %1").arg(expirationDate)
: expired ? tr("The selected task is expired since %1").arg(
expirationDate)
: tr("The selected task is not trackable");
m_ui->taskStatusLB->setText(info);
}
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_selectedTask != 0);
}
bool SelectTaskDialog::isValidAndTrackable(const QModelIndex &index) const
{
if (!index.isValid())
return false;
const Task task = m_proxy.taskForIndex(index);
const bool taskValid = m_nonValidSelectable || (task.isValid() && task.isCurrentlyValid());
if (m_nonTrackableSelectable)
return taskValid;
return taskValid && task.trackable();
}
void SelectTaskDialog::slotDoubleClicked(const QModelIndex &index)
{
if (isValidAndTrackable(index))
accept();
}
void SelectTaskDialog::slotFilterTextChanged(const QString &text)
{
QString filtertext = text.simplified();
filtertext.replace(QLatin1Char(' '), QLatin1Char('*'));
Charm::saveExpandStates(m_ui->treeView, &m_expansionStates);
m_proxy.setFilterWildcard(filtertext);
if (!filtertext.isEmpty()) {
m_ui->treeView->expandAll();
} else {
Charm::restoreExpandStates(m_ui->treeView, &m_expansionStates);
}
}
void SelectTaskDialog::slotAccepted()
{
QSettings settings;
// FIXME refactor, code duplication with taskview
// save user settings
if (ApplicationCore::instance().state() == Connected
|| ApplicationCore::instance().state() == Disconnecting) {
GUIState state;
// selected task
state.setSelectedTask(selectedTask());
// expanded tasks
TaskList tasks = MODEL.charmDataModel()->getAllTasks();
TaskIdList expandedTasks;
Q_FOREACH (const Task &task, tasks) {
QModelIndex index(m_proxy.indexForTaskId(task.id()));
if (m_ui->treeView->isExpanded(index))
expandedTasks << task.id();
}
state.setExpandedTasks(expandedTasks);
state.setShowExpired(m_ui->showExpired->isChecked());
state.setShowCurrents(m_ui->showSelected->isChecked());
settings.beginGroup(QString::fromUtf8(staticMetaObject.className()));
state.saveTo(settings);
}
}
void SelectTaskDialog::slotPrefilteringChanged()
{
// find out about the selected mode:
Configuration::TaskPrefilteringMode mode;
const bool showCurrentOnly = !m_ui->showExpired->isChecked();
const bool showSubscribedOnly = m_ui->showSelected->isChecked();
if (showCurrentOnly && showSubscribedOnly) {
mode = Configuration::TaskPrefilter_SubscribedAndCurrentOnly;
} else if (showCurrentOnly && !showSubscribedOnly) {
mode = Configuration::TaskPrefilter_CurrentOnly;
} else if (!showCurrentOnly && showSubscribedOnly) {
mode = Configuration::TaskPrefilter_SubscribedOnly;
} else {
mode = Configuration::TaskPrefilter_ShowAll;
}
CONFIGURATION.taskPrefilteringMode = mode;
Charm::saveExpandStates(m_ui->treeView, &m_expansionStates);
m_proxy.prefilteringModeChanged();
Charm::restoreExpandStates(m_ui->treeView, &m_expansionStates);
emit saveConfiguration();
}
void SelectTaskDialog::setNonTrackableSelectable()
{
m_nonTrackableSelectable = true;
}
void SelectTaskDialog::setNonValidSelectable()
{
m_nonValidSelectable = true;
}
void SelectTaskDialog::slotSelectTask(const QString &filter)
{
const QString filterText = filter.simplified().toUpper().replace(QLatin1Char('*'), QLatin1Char(' '));
const int filterTaskId = filterText.toInt();
const TaskList tasks = MODEL.charmDataModel()->getAllTasks();
for (const auto task : tasks) {
if (!task.isValid())
continue;
if (task.name().toUpper().contains(filterText) || task.id() == filterTaskId)
selectTask(task.id());
}
}
|