- refactor task properties to dedicated widget

This commit is contained in:
Dmytro Bogovych 2022-06-05 19:17:17 +03:00
parent 37e70ab252
commit 6bc7985b44
10 changed files with 383 additions and 288 deletions

View File

@ -7,253 +7,261 @@
#include <QFileDialog> #include <QFileDialog>
AttachmentsListModel::AttachmentsListModel(PTask task, ChangesHistory& history, const AttachmentArray &items, QObject *parent) AttachmentsListModel::AttachmentsListModel(PTask task, ChangesHistory& history, const AttachmentArray &items, QObject *parent)
:QAbstractListModel(parent), mTask(task), mHistory(history), mData(items) :QAbstractListModel(parent), mTask(task), mHistory(history), mData(items)
{ {
} }
int AttachmentsListModel::rowCount(const QModelIndex &parent) const int AttachmentsListModel::rowCount(const QModelIndex &parent) const
{ {
return mData.size(); return mData.size();
} }
QVariant AttachmentsListModel::data(const QModelIndex& index, int role) const QVariant AttachmentsListModel::data(const QModelIndex& index, int role) const
{ {
// Check that the index is valid and within the correct range first: // Check that the index is valid and within the correct range first:
if (!index.isValid()) if (!index.isValid())
return QVariant(); return QVariant();
if (index.row() >= mData.size()) if (index.row() >= mData.size())
return QVariant(); return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) if (role == Qt::DisplayRole || role == Qt::EditRole)
{ {
// Only returns something for the roles you support (DisplayRole is a minimum) // Only returns something for the roles you support (DisplayRole is a minimum)
return QVariant(mData.at(index.row())->filename()); return QVariant(mData.at(index.row())->filename());
} }
else else
if (role == Qt::DecorationRole) if (role == Qt::DecorationRole)
{ {
QFileInfo fi(mData.at(index.row())->filename()); QFileInfo fi(mData.at(index.row())->filename());
QIcon icon = mIconProvider.icon(fi); QIcon icon = mIconProvider.icon(fi);
return QVariant(icon); return QVariant(icon);
} }
else else
return QVariant(); return QVariant();
} }
Qt::ItemFlags AttachmentsListModel::flags(const QModelIndex &index) const Qt::ItemFlags AttachmentsListModel::flags(const QModelIndex &index) const
{ {
Qt::ItemFlags result = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;// | Qt::ItemIsDropEnabled; Qt::ItemFlags result = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;// | Qt::ItemIsDropEnabled;
//if (index.isValid()) //if (index.isValid())
// result |= Qt::ItemIsDragEnabled; // result |= Qt::ItemIsDragEnabled;
return result; return result;
} }
bool AttachmentsListModel::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */) bool AttachmentsListModel::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
{ {
PAttachment att = itemAt(index.row()); PAttachment att = itemAt(index.row());
switch (role) switch (role)
{ {
case Qt::DisplayRole: case Qt::DisplayRole:
case Qt::EditRole: case Qt::EditRole:
MAKE_ACTION(new RenameAttachmentAction(mTask, att, value.toString())); MAKE_ACTION(new RenameAttachmentAction(mTask, att, value.toString()));
break; break;
} }
return true; return true;
} }
void AttachmentsListModel::addItem(PAttachment att, int index) void AttachmentsListModel::addItem(PAttachment att, int index)
{ {
if (index == -1) if (index == -1)
{ {
beginInsertRows(QModelIndex(), rowCount(), rowCount()); beginInsertRows(QModelIndex(), rowCount(), rowCount());
mData.push_back(att); mData.push_back(att);
endInsertRows(); endInsertRows();
} }
else else
{ {
beginInsertRows(QModelIndex(), index, index); beginInsertRows(QModelIndex(), index, index);
mData.insert(mData.begin() + index, att); mData.insert(mData.begin() + index, att);
endInsertRows(); endInsertRows();
} }
} }
void AttachmentsListModel::removeItem(int row) void AttachmentsListModel::removeItem(int row)
{ {
beginRemoveRows(QModelIndex(), row, row); beginRemoveRows(QModelIndex(), row, row);
mData.erase(mData.begin() + row); mData.erase(mData.begin() + row);
endRemoveRows(); endRemoveRows();
} }
PAttachment AttachmentsListModel::itemAt(int row) const PAttachment AttachmentsListModel::itemAt(int row) const
{ {
return mData[row]; return mData[row];
} }
int AttachmentsListModel::findRow(PAttachment att) const int AttachmentsListModel::findRow(PAttachment att) const
{ {
return mData.indexOf(att); return mData.indexOf(att);
} }
AttachmentsList::AttachmentsList(QWidget *parent) : AttachmentsList::AttachmentsList(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::AttachmentsList) ui(new Ui::AttachmentsList)
{ {
ui->setupUi(this); ui->setupUi(this);
} }
AttachmentsList::~AttachmentsList() AttachmentsList::~AttachmentsList()
{ {
delete ui; delete ui;
} }
void AttachmentsList::setTask(PTask task) void AttachmentsList::setTask(const PTask& task)
{ {
mTask = task; if (mTask != task)
AttachmentArray aa; {
Storage::instance().loadAttachments(mTask, aa); mTask = task;
AttachmentArray aa;
Storage::instance().loadAttachments(mTask, aa);
// Create model // Create model
mModel = new AttachmentsListModel(mTask, mHistory, aa); mModel = new AttachmentsListModel(mTask, mHistory, aa);
// Set model to history // Set model to history
mHistory.setAttachmentsModel(mModel); mHistory.setAttachmentsModel(mModel);
// Set model to list view // Set model to list view
ui->mListView->setModel(mModel); ui->mListView->setModel(mModel);
}
}
PTask AttachmentsList::task() const
{
return mTask;
} }
void AttachmentsList::setParentWidget(QWidget *w) void AttachmentsList::setParentWidget(QWidget *w)
{ {
mParentWidget = w; mParentWidget = w;
} }
void AttachmentsList::updateActionsState() void AttachmentsList::updateActionsState()
{ {
bool hasSelectedItem = ui->mListView->currentIndex().isValid(); bool hasSelectedItem = ui->mListView->currentIndex().isValid();
ui->mRenameAction->setEnabled(hasSelectedItem); ui->mRenameAction->setEnabled(hasSelectedItem);
ui->mDeleteAction->setEnabled(hasSelectedItem); ui->mDeleteAction->setEnabled(hasSelectedItem);
ui->mExportAction->setEnabled(hasSelectedItem); ui->mExportAction->setEnabled(hasSelectedItem);
} }
void AttachmentsList::contextualMenu(const QPoint& point) void AttachmentsList::contextualMenu(const QPoint& point)
{ {
updateActionsState(); updateActionsState();
QMenu* menu = new QMenu(); QMenu* menu = new QMenu();
menu->addAction(ui->mRenameAction); menu->addAction(ui->mRenameAction);
menu->addAction(ui->mDeleteAction); menu->addAction(ui->mDeleteAction);
menu->addAction(ui->mExportAction); menu->addAction(ui->mExportAction);
menu->addAction(ui->mImportAction); menu->addAction(ui->mImportAction);
//menu->addAction(tr("Add 10 mins to timeline"), this, SLOT(add10Mins())); //menu->addAction(tr("Add 10 mins to timeline"), this, SLOT(add10Mins()));
menu->exec(this->window()->mapToGlobal(point)); menu->exec(this->window()->mapToGlobal(point));
} }
void AttachmentsList::importFile() void AttachmentsList::importFile()
{ {
// Prepare file open dialog // Prepare file open dialog
QFileDialog dlg(mParentWidget, Qt::Sheet); QFileDialog dlg(mParentWidget, Qt::Sheet);
dlg.setWindowTitle(tr("Select file(s) for import")); dlg.setWindowTitle(tr("Select file(s) for import"));
dlg.setAcceptDrops(false); dlg.setAcceptDrops(false);
dlg.setAcceptMode(QFileDialog::AcceptOpen); dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setFileMode(QFileDialog::ExistingFiles); dlg.setFileMode(QFileDialog::ExistingFiles);
if (!dlg.exec()) if (!dlg.exec())
return; return;
// Iterate selected files // Iterate selected files
QStringList files = dlg.selectedFiles(); QStringList files = dlg.selectedFiles();
for (QString filename: files) for (QString filename: files)
{
QFile f(filename);
f.open(QFile::ReadOnly);
if (f.isOpen())
{ {
// Get data from file QFile f(filename);
QByteArray content = f.readAll(); f.open(QFile::ReadOnly);
if (f.isOpen())
{
// Get data from file
QByteArray content = f.readAll();
// Compress them // Compress them
QByteArray compressed = qCompress(content); QByteArray compressed = qCompress(content);
// Put it to Attachment instance // Put it to Attachment instance
QFileInfo fi(filename); QFileInfo fi(filename);
PAttachment att(new Attachment()); PAttachment att(new Attachment());
att->setTaskId(mTask->id()) att->setTaskId(mTask->id())
.setIndex(mModel->rowCount()) .setIndex(mModel->rowCount())
.setFilename(fi.fileName()); .setFilename(fi.fileName());
// Save everything // Save everything
att->saveMetadata() att->saveMetadata()
.saveContent(compressed); .saveContent(compressed);
mModel->addItem(att); mModel->addItem(att);
}
f.close();
} }
f.close();
}
// Refresh AttachmentsCount property on owner task // Refresh AttachmentsCount property on owner task
mTask->preloadAttachmentCount(); mTask->preloadAttachmentCount();
} }
void AttachmentsList::exportFile() void AttachmentsList::exportFile()
{ {
QModelIndexList mil = ui->mListView->selectionModel()->selectedIndexes(); QModelIndexList mil = ui->mListView->selectionModel()->selectedIndexes();
foreach (const QModelIndex& index, mil) foreach (const QModelIndex& index, mil)
{ {
if (!index.isValid()) if (!index.isValid())
continue; continue;
QFileDialog dlg(mParentWidget, Qt::Sheet); QFileDialog dlg(mParentWidget, Qt::Sheet);
dlg.setWindowTitle(tr("Select file(s) for export")); dlg.setWindowTitle(tr("Select file(s) for export"));
dlg.setAcceptDrops(false); dlg.setAcceptDrops(false);
dlg.setAcceptMode(QFileDialog::AcceptSave); dlg.setAcceptMode(QFileDialog::AcceptSave);
PAttachment att = mModel->itemAt(index.row()); PAttachment att = mModel->itemAt(index.row());
dlg.selectFile(att->filename()); dlg.selectFile(att->filename());
if (!dlg.exec()) if (!dlg.exec())
continue; continue;
QFile outputFile(dlg.selectedFiles().front()); QFile outputFile(dlg.selectedFiles().front());
outputFile.open(QFile::WriteOnly); outputFile.open(QFile::WriteOnly);
outputFile.write(qUncompress(att->loadContent())); outputFile.write(qUncompress(att->loadContent()));
outputFile.close(); outputFile.close();
} }
} }
void AttachmentsList::deleteFile() void AttachmentsList::deleteFile()
{ {
QModelIndexList mil = ui->mListView->selectionModel()->selectedIndexes(); QModelIndexList mil = ui->mListView->selectionModel()->selectedIndexes();
foreach (const QModelIndex& index, mil) foreach (const QModelIndex& index, mil)
{
if (!index.isValid())
continue;
PAttachment att = mModel->itemAt(index.row());
// Remove from DB
Storage::instance().deleteAttachment(att);
// Remove from model
mModel->removeItem(index.row());
// Iterate other items and decrease their DB table's orderid field
for (int row = index.row() + 1; row < mModel->rowCount(); row++)
{ {
Attachment& att = *mModel->itemAt(row); if (!index.isValid())
att.setIndex(att.index() - 1) continue;
.saveMetadata(); PAttachment att = mModel->itemAt(index.row());
}
}
// Refresh hasAttachment property value on task // Remove from DB
mTask->preloadAttachmentCount(); Storage::instance().deleteAttachment(att);
// Remove from model
mModel->removeItem(index.row());
// Iterate other items and decrease their DB table's orderid field
for (int row = index.row() + 1; row < mModel->rowCount(); row++)
{
Attachment& att = *mModel->itemAt(row);
att.setIndex(att.index() - 1)
.saveMetadata();
}
}
// Refresh hasAttachment property value on task
mTask->preloadAttachmentCount();
} }
void AttachmentsList::renameFile() void AttachmentsList::renameFile()
{ {
QModelIndex index = ui->mListView->currentIndex(); QModelIndex index = ui->mListView->currentIndex();
if (index.isValid()) if (index.isValid())
ui->mListView->edit(index); ui->mListView->edit(index);
} }

View File

@ -42,7 +42,8 @@ class AttachmentsList : public QWidget
public: public:
explicit AttachmentsList(QWidget *parent = 0); explicit AttachmentsList(QWidget *parent = 0);
~AttachmentsList(); ~AttachmentsList();
void setTask(PTask task); void setTask(const PTask& task);
PTask task() const;
void setParentWidget(QWidget* w); void setParentWidget(QWidget* w);
private: private:

View File

@ -41,8 +41,6 @@
#include <QDebug> #include <QDebug>
#include <iostream> #include <iostream>
#define SETTINGS mSettings->data()
const int ViewIndex_Main = 0; const int ViewIndex_Main = 0;
const int ViewIndex_OpenOrCreateDb = 1; const int ViewIndex_OpenOrCreateDb = 1;
const int ViewIndex_DbPassword = 2; const int ViewIndex_DbPassword = 2;
@ -51,10 +49,8 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
mPasswordFailed(false), mFindInTasksDlg(this), mDockRecentMenu(nullptr) mPasswordFailed(false), mFindInTasksDlg(this), mDockRecentMenu(nullptr)
{ {
mSettings = QSharedPointer<Settings>(new Settings());
// Dark theme if needed // Dark theme if needed
helper::theme::applyCurrent(*mSettings); helper::theme::applyCurrent(SETTINGS);
// Restore window size & position from last run // Restore window size & position from last run
setUpdatesEnabled(false); setUpdatesEnabled(false);
@ -75,7 +71,7 @@ MainWindow::~MainWindow()
void MainWindow::attachDatabase() void MainWindow::attachDatabase()
{ {
// Find default database file exists // Find default database file exists
QString path = mSettings->getDatabasePath(); QString path = SETTINGS.getDatabasePath();
QString folder = QFileInfo(path).absoluteDir().path(); QString folder = QFileInfo(path).absoluteDir().path();
Storage::instance().setPath(path); Storage::instance().setPath(path);
@ -84,7 +80,7 @@ void MainWindow::attachDatabase()
askNewDbPassword(); askNewDbPassword();
else else
{ {
if (mSettings->data()[KEY_AUTOSAVE_PASSWORD].toBool()) if (SETTINGS.data()[KEY_AUTOSAVE_PASSWORD].toBool())
{ {
QString password = helper::password::loadFromKeychain(); QString password = helper::password::loadFromKeychain();
if (password.isEmpty()) if (password.isEmpty())
@ -120,11 +116,11 @@ void MainWindow::connectUiToDatabase()
if (mTaskTreeModel->hasChildren()) if (mTaskTreeModel->hasChildren())
ui->mTaskTree->setCurrentIndex(mTaskTreeModel->index(0,0, QModelIndex())); ui->mTaskTree->setCurrentIndex(mTaskTreeModel->index(0,0, QModelIndex()));
mTaskTreeModel->setExpandedState(mSettings->data()[KEY_EXPANDED_TASKS].toString(), *ui->mTaskTree); mTaskTreeModel->setExpandedState(SETTINGS.data()[KEY_EXPANDED_TASKS].toString(), *ui->mTaskTree);
mTaskTreeModel->setSelected(mSettings->data()[KEY_SELECTED_TASKS].toString(), *ui->mTaskTree); mTaskTreeModel->setSelected(SETTINGS.data()[KEY_SELECTED_TASKS].toString(), *ui->mTaskTree);
// Load recent tasks // Load recent tasks
QString recent = mSettings->data()[KEY_RECENT_TASKS].toString(); QString recent = SETTINGS.data()[KEY_RECENT_TASKS].toString();
QStringList recentList = recent.split(";", QString::SkipEmptyParts); QStringList recentList = recent.split(";", QString::SkipEmptyParts);
for (QString& s: recentList) for (QString& s: recentList)
{ {
@ -182,7 +178,7 @@ void MainWindow::initClient()
ui->mCheckForUpdatesAction->setMenuRole(QAction::ApplicationSpecificRole); ui->mCheckForUpdatesAction->setMenuRole(QAction::ApplicationSpecificRole);
// Build tray icon if system support this one // Build tray icon if system support this one
if (QSystemTrayIcon::isSystemTrayAvailable() && mSettings->data()[KEY_SHOW_TRAY_ICON].toBool()) if (QSystemTrayIcon::isSystemTrayAvailable() && SETTINGS.data()[KEY_SHOW_TRAY_ICON].toBool())
initTrayIcon(); initTrayIcon();
setWindowIcon(getAppIcon()); setWindowIcon(getAppIcon());
@ -295,7 +291,7 @@ QSharedPointer<QByteArray> MainWindow::getUndoStack() const
void MainWindow::saveGeometry() void MainWindow::saveGeometry()
{ {
AppGeometry g(*mSettings); AppGeometry g(SETTINGS);
g.setMaximized(isMaximized()); g.setMaximized(isMaximized());
g.setWindowRect(this->geometry()); g.setWindowRect(this->geometry());
g.setSplitterPos(0, {ui->mSplitter->sizes().front(), ui->mSplitter->sizes().back()}); g.setSplitterPos(0, {ui->mSplitter->sizes().front(), ui->mSplitter->sizes().back()});
@ -303,8 +299,8 @@ void MainWindow::saveGeometry()
if (mTaskTreeModel) if (mTaskTreeModel)
{ {
mSettings->data()[KEY_EXPANDED_TASKS] = this->mTaskTreeModel->getExpandedState(*ui->mTaskTree); SETTINGS.data()[KEY_EXPANDED_TASKS] = this->mTaskTreeModel->getExpandedState(*ui->mTaskTree);
mSettings->data()[KEY_SELECTED_TASKS] = this->mTaskTreeModel->getSelected(*ui->mTaskTree); SETTINGS.data()[KEY_SELECTED_TASKS] = this->mTaskTreeModel->getSelected(*ui->mTaskTree);
} }
// Save recent list // Save recent list
@ -313,13 +309,13 @@ void MainWindow::saveGeometry()
{ {
recent += QString::number(t->id()) + ";"; recent += QString::number(t->id()) + ";";
} }
mSettings->data()[KEY_RECENT_TASKS] = recent; SETTINGS.data()[KEY_RECENT_TASKS] = recent;
mSettings->save(); SETTINGS.save();
} }
void MainWindow::loadGeometry() void MainWindow::loadGeometry()
{ {
auto g = AppGeometry(*mSettings); auto g = AppGeometry(SETTINGS);
if (g.isMaximized()) if (g.isMaximized())
this->showMaximized(); this->showMaximized();
@ -415,7 +411,7 @@ void MainWindow::onDatabaseAvailable()
void MainWindow::preferences() void MainWindow::preferences()
{ {
PreferencesDlg pref(this, *mSettings); PreferencesDlg pref(this, SETTINGS);
if (pref.exec() == QDialog::Accepted) if (pref.exec() == QDialog::Accepted)
{ {
@ -428,20 +424,20 @@ void MainWindow::preferences()
emit onTimeFormatChanged(); emit onTimeFormatChanged();
// Delete autosaved password if needed // Delete autosaved password if needed
if (mSettings->data()[KEY_AUTOSAVE_PASSWORD].toBool() == false) if (SETTINGS.data()[KEY_AUTOSAVE_PASSWORD].toBool() == false)
{ {
// Reset password in keychain // Reset password in keychain
helper::password::saveToKeychain(QString("")); helper::password::saveToKeychain(QString(""));
//mSettings->data()[KEY_PASSWORD] = NOPASSWORDSTRING; //mSettings->data()[KEY_PASSWORD] = NOPASSWORDSTRING;
mSettings->save(); SETTINGS.save();
} }
// Hide/Show tray icon if need // Hide/Show tray icon if need
if (mSettings->data()[KEY_SHOW_TRAY_ICON].toBool() && !mTrayIcon && QSystemTrayIcon::isSystemTrayAvailable()) if (SETTINGS.data()[KEY_SHOW_TRAY_ICON].toBool() && !mTrayIcon && QSystemTrayIcon::isSystemTrayAvailable())
initTrayIcon(); initTrayIcon();
else else
if (!mSettings->data()[KEY_SHOW_TRAY_ICON].toBool() && mTrayIcon) if (!SETTINGS.data()[KEY_SHOW_TRAY_ICON].toBool() && mTrayIcon)
removeTrayIcon(); removeTrayIcon();
updateData(); updateData();
@ -573,7 +569,7 @@ void MainWindow::deleteTask()
} }
else else
{ {
if (mSettings->data()[KEY_ASK_BEFORE_DELETE].toBool()) if (SETTINGS.data()[KEY_ASK_BEFORE_DELETE].toBool())
{ {
auto reply = QMessageBox::question(this, auto reply = QMessageBox::question(this,
tr("Are you sure?"), tr("Are you sure?"),
@ -714,9 +710,9 @@ void MainWindow::idleDetected()
mActivityTracker->acceptIdleState(); mActivityTracker->acceptIdleState();
// Check if settings allow smart stop // Check if settings allow smart stop
if (SETTINGS[KEY_SMART_STOP].toBool() && SETTINGS[KEY_SMART_STOP_MINUTES].toInt() > 0) if (SETTINGS.data()[KEY_SMART_STOP].toBool() && SETTINGS.data()[KEY_SMART_STOP_MINUTES].toInt() > 0)
{ {
if (SETTINGS[KEY_ASK_STOP].toBool()) if (SETTINGS.data()[KEY_ASK_STOP].toBool())
{ {
// Stop activity tracker to not be confused with following user activity // Stop activity tracker to not be confused with following user activity
mActivityTracker->stop(); mActivityTracker->stop();
@ -736,7 +732,7 @@ void MainWindow::activityDetected()
mActivityTracker->acceptUserActiveState(); mActivityTracker->acceptUserActiveState();
// Check if settings allow smart start // Check if settings allow smart start
if (SETTINGS[KEY_SMART_START].toBool() && mStopReason == TSR_Automatic) if (SETTINGS.data()[KEY_SMART_START].toBool() && mStopReason == TSR_Automatic)
{ {
/*if (SETTINGS[KEY_ASK_START].toBool()) /*if (SETTINGS[KEY_ASK_START].toBool())
{ {
@ -902,9 +898,9 @@ void MainWindow::startTracking(PTask t)
updateTrayIcon(Tray_ShowMessage); updateTrayIcon(Tray_ShowMessage);
// Start activity tracker if needed // Start activity tracker if needed
if (mSettings->data()[KEY_SMART_STOP].toBool() && mSettings->data()[KEY_SMART_STOP_MINUTES].toInt() > 0) if (SETTINGS.data()[KEY_SMART_STOP].toBool() && SETTINGS.data()[KEY_SMART_STOP_MINUTES].toInt() > 0)
{ {
mActivityTracker->setInterval(mSettings->data()[KEY_SMART_STOP_MINUTES].toInt() * 60); mActivityTracker->setInterval(SETTINGS.data()[KEY_SMART_STOP_MINUTES].toInt() * 60);
mActivityTracker->start(); mActivityTracker->start();
} }
} }
@ -917,7 +913,7 @@ void MainWindow::startTracking()
return; return;
// Trigger permission dialog if needed // Trigger permission dialog if needed
if (mSettings->data()[KEY_SMART_STOP].toBool()) if (SETTINGS.data()[KEY_SMART_STOP].toBool())
{ {
if (!helper::activityTracker::ensureSmartTrackingIsPossible()) if (!helper::activityTracker::ensureSmartTrackingIsPossible())
mTrayIcon->showMessage(tr("No smart tracking stop/start"), tr("Problem with obtaining permissions"), QSystemTrayIcon::Warning); mTrayIcon->showMessage(tr("No smart tracking stop/start"), tr("Problem with obtaining permissions"), QSystemTrayIcon::Warning);
@ -1296,26 +1292,14 @@ void MainWindow::timeFormatChanged()
void MainWindow::showTimeForSelectedTask() void MainWindow::showTimeForSelectedTask()
{ {
PTask t = mTaskTreeModel->getTask(ui->mTaskTree->currentIndex()); PTask t = mTaskTreeModel->getTask(ui->mTaskTree->currentIndex());
bool showSeconds = mSettings->data()[KEY_SHOW_SECONDS].toBool(); ui->mProperties->setTask(t);
// Show stats for current task
if (t)
{
if (!t->timeline())
t->loadContent();
int spentSecondsToday = t->timeline()->today();
int spentSecondsMonth = t->timeline()->month();
ui->mTodaySpentTimeLabel->setText(QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsToday, showSeconds)));
ui->mThisMonthSpentTimeLabel->setText(QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsMonth, showSeconds)));
}
} }
void MainWindow::showTimeForTrackingTask() void MainWindow::showTimeForTrackingTask()
{ {
if (mCurrentTask) if (mCurrentTask)
{ {
bool showSeconds = mSettings->data()[KEY_SHOW_SECONDS].toBool(); bool showSeconds = SETTINGS.data()[KEY_SHOW_SECONDS].toBool();
// Update status bar // Update status bar
QString path; QString path;
@ -1369,7 +1353,7 @@ void MainWindow::updateTrayIcon(TrayShowMessage flag)
QString tooltip; QString tooltip;
if (mCurrentTask) if (mCurrentTask)
{ {
bool showSeconds = mSettings->data()[KEY_SHOW_SECONDS].toBool(); bool showSeconds = SETTINGS.data()[KEY_SHOW_SECONDS].toBool();
int spentSecondsToday = mCurrentTask->timeline()->today(); int spentSecondsToday = mCurrentTask->timeline()->today();
QString timeString = QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsToday, showSeconds)); QString timeString = QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsToday, showSeconds));
tooltip = tr("Noo is tracking ") + mCurrentTask->title() + ".\n" + tooltip = tr("Noo is tracking ") + mCurrentTask->title() + ".\n" +
@ -1408,7 +1392,7 @@ void MainWindow::showTimeline()
return; return;
PTask t = mTaskTreeModel->getTask(ui->mTaskTree->currentIndex()); PTask t = mTaskTreeModel->getTask(ui->mTaskTree->currentIndex());
TimeTreeDlg dlg(this, t->timeline(), *mSettings); TimeTreeDlg dlg(this, t->timeline(), SETTINGS);
dlg.exec(); dlg.exec();
// Refresh current timeline stats // Refresh current timeline stats
@ -1417,7 +1401,7 @@ void MainWindow::showTimeline()
void MainWindow::showTimeReport() void MainWindow::showTimeReport()
{ {
TimeReportWizard trz(*mSettings, this); TimeReportWizard trz(SETTINGS, this);
trz.exec(); trz.exec();
} }
@ -1652,7 +1636,7 @@ void MainWindow::trayWindowDestroyed(QObject */*object*/)
void MainWindow::onDbPasswordEntered(const QString& password) void MainWindow::onDbPasswordEntered(const QString& password)
{ {
// Save password to keychain if needed // Save password to keychain if needed
if (mSettings->data()[KEY_AUTOSAVE_PASSWORD].toBool()) if (SETTINGS.data()[KEY_AUTOSAVE_PASSWORD].toBool())
helper::password::saveToKeychain(password); helper::password::saveToKeychain(password);
// Try to open database // Try to open database
@ -1674,7 +1658,7 @@ void MainWindow::onDbPasswordCancelled()
void MainWindow::onNewDbPasswordEntered(const QString& password) void MainWindow::onNewDbPasswordEntered(const QString& password)
{ {
// Save password to keychain if needed // Save password to keychain if needed
if (mSettings->data()[KEY_AUTOSAVE_PASSWORD].toBool()) if (SETTINGS.data()[KEY_AUTOSAVE_PASSWORD].toBool())
helper::password::saveToKeychain(password); helper::password::saveToKeychain(password);
// Configure storage // Configure storage
@ -1695,8 +1679,8 @@ void MainWindow::onNewDbPasswordEntered(const QString& password)
void MainWindow::onDatabaseChanged(const QString& path) void MainWindow::onDatabaseChanged(const QString& path)
{ {
// Bind to specific database // Bind to specific database
mSettings->data()[KEY_DB_FILENAME] = path; SETTINGS.data()[KEY_DB_FILENAME] = path;
mSettings->save(); SETTINGS.save();
Storage::instance().setPath(path); Storage::instance().setPath(path);
// Try to open database // Try to open database

View File

@ -63,7 +63,6 @@ private:
QLabel* mCurrentIntervalLabel = nullptr; QLabel* mCurrentIntervalLabel = nullptr;
QLabel* mDuplicationSignalLabel = nullptr;; QLabel* mDuplicationSignalLabel = nullptr;;
QSystemTrayIcon *mTrayIcon = nullptr; QSystemTrayIcon *mTrayIcon = nullptr;
QSharedPointer<Settings> mSettings;
bool mPasswordFailed = false; bool mPasswordFailed = false;
PasswordDlg* mPasswordDlg = nullptr; PasswordDlg* mPasswordDlg = nullptr;
NewPasswordDlg* mNewPasswordDlg = nullptr; NewPasswordDlg* mNewPasswordDlg = nullptr;

View File

@ -81,36 +81,9 @@
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Raised</enum> <enum>QFrame::Raised</enum>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin"> <item>
<number>2</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QSplitter" name="mTimeSplitter"> <widget class="QSplitter" name="mTimeSplitter">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>4000</height>
</size>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@ -210,59 +183,7 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QFrame" name="mTimeFrame"> <widget class="NodePropertiesWidget" name="mProperties" native="true"/>
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>4000</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="mTodayTextLabel">
<property name="text">
<string>Today:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="mTodaySpentTimeLabel">
<property name="text">
<string>0 hours 0 minutes</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mThisMonthTextLabel">
<property name="text">
<string>This month:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="mThisMonthSpentTimeLabel">
<property name="text">
<string>0 hours 0 minutes</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -771,6 +692,12 @@
<extends>QPlainTextEdit</extends> <extends>QPlainTextEdit</extends>
<header>qmarkdowntextedit/qmarkdowntextedit.h</header> <header>qmarkdowntextedit/qmarkdowntextedit.h</header>
</customwidget> </customwidget>
<customwidget>
<class>NodePropertiesWidget</class>
<extends>QWidget</extends>
<header>nodepropertieswidget.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="mainwindow.qrc"/> <include location="mainwindow.qrc"/>

View File

@ -0,0 +1,52 @@
#include "nodepropertieswidget.h"
#include "ui_nodepropertieswidget.h"
#include "settings.h"
NodePropertiesWidget::NodePropertiesWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::NodePropertiesWidget)
{
ui->setupUi(this);
}
NodePropertiesWidget::~NodePropertiesWidget()
{
delete ui;
}
void NodePropertiesWidget::setTask(const PTask& task)
{
if (mTask != task)
{
mTask = task;
update();
}
}
PTask NodePropertiesWidget::task() const
{
return mTask;
}
void NodePropertiesWidget::refresh()
{
bool showSeconds = SETTINGS.data()[KEY_SHOW_SECONDS].toBool();
if (mTask != nullptr)
{
ui->mFileListView->setTask(mTask);
if (!mTask->timeline())
mTask->loadContent();
int spentSecondsToday = mTask->timeline()->today();
int spentSecondsMonth = mTask->timeline()->month();
ui->mTodaySpentTimeLabel->setText(QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsToday, showSeconds)));
ui->mThisMonthSpentTimeLabel->setText(QString::fromStdString(helper::chrono::secondsToDisplay(spentSecondsMonth, showSeconds)));
}
else
{
ui->mTimeFrame->setVisible(false);
ui->mFileListView->setTask(PTask());
}
}

View File

@ -0,0 +1,29 @@
#ifndef NODEPROPERTIESWIDGET_H
#define NODEPROPERTIESWIDGET_H
#include <QWidget>
#include "task.h"
namespace Ui {
class NodePropertiesWidget;
}
class NodePropertiesWidget : public QWidget
{
Q_OBJECT
public:
explicit NodePropertiesWidget(QWidget *parent = nullptr);
~NodePropertiesWidget();
void setTask(const PTask& task);
PTask task() const;
private:
Ui::NodePropertiesWidget *ui;
void refresh();
PTask mTask;
};
#endif // NODEPROPERTIESWIDGET_H

View File

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>NodePropertiesWidget</class>
<widget class="QWidget" name="NodePropertiesWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>449</width>
<height>470</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QFrame" name="mTimeFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>4000</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="mTodayTextLabel">
<property name="text">
<string>Today:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="mTodaySpentTimeLabel">
<property name="text">
<string>0 hours 0 minutes</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mThisMonthTextLabel">
<property name="text">
<string>This month:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="mThisMonthSpentTimeLabel">
<property name="text">
<string>0 hours 0 minutes</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="AttachmentsList" name="mFileListView" native="true"/>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>AttachmentsList</class>
<extends>QWidget</extends>
<header>attachmentslist.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -52,6 +52,7 @@ INCLUDEPATH += $$PWD/../lib/include
SOURCES += main.cpp \ SOURCES += main.cpp \
mainwindow.cpp \ mainwindow.cpp \
nodepropertieswidget.cpp \
storage.cpp \ storage.cpp \
task.cpp \ task.cpp \
tasktreemodel.cpp \ tasktreemodel.cpp \
@ -91,6 +92,7 @@ SOURCES += main.cpp \
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
nodepropertieswidget.h \
storage.h \ storage.h \
task.h \ task.h \
tasktreemodel.h \ tasktreemodel.h \
@ -150,6 +152,7 @@ unix {
FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
newpassworddlg.ui \ newpassworddlg.ui \
nodepropertieswidget.ui \
passworddlg.ui \ passworddlg.ui \
preferencesdlg.ui \ preferencesdlg.ui \
timetreedlg.ui \ timetreedlg.ui \

View File

@ -61,6 +61,8 @@ protected:
QVariantMap mData; QVariantMap mData;
}; };
#define SETTINGS Settings::instance()
class AppGeometry class AppGeometry
{ {
public: public: