File indexing completed on 2024-05-19 05:21:53
0001 /* 0002 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net> 0003 * Copyright (C) 2019 Alexander Potashev <aspotashev@gmail.com> 0004 * 0005 * This program is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License along 0016 * with this program; if not, write to the 0017 * Free Software Foundation, Inc. 0018 * 51 Franklin Street, Fifth Floor 0019 * Boston, MA 02110-1301 USA. 0020 * 0021 */ 0022 0023 #include "mainwindow.h" 0024 0025 #include <QGuiApplication> 0026 #include <QMenu> 0027 #include <QStatusBar> 0028 0029 #include <KActionCollection> 0030 #include <KLocalizedString> 0031 #include <KXMLGUIFactory> 0032 0033 #include "ktimetracker.h" 0034 #include "ktimetrackerutility.h" 0035 #include "ktt_debug.h" 0036 #include "model/task.h" 0037 #include "timetrackerwidget.h" 0038 #include "tray.h" 0039 0040 MainWindow::MainWindow(const QUrl &url) 0041 : KXmlGuiWindow(nullptr) 0042 , m_tray(nullptr) 0043 , m_mainWidget(nullptr) 0044 , m_quitRequested(false) 0045 { 0046 qCDebug(KTT_LOG) << "Entering function, url is " << url; 0047 0048 m_mainWidget = new TimeTrackerWidget(this); 0049 setCentralWidget(m_mainWidget); 0050 0051 auto *configureAction = new QAction(this); 0052 configureAction->setText(i18nc("@action:inmenu", "Configure KTimeTracker...")); 0053 actionCollection()->addAction(QStringLiteral("configure_ktimetracker"), configureAction); 0054 connect(configureAction, &QAction::triggered, m_mainWidget, &TimeTrackerWidget::showSettingsDialog); 0055 m_mainWidget->setupActions(actionCollection()); 0056 0057 KStandardAction::quit(this, &MainWindow::quit, actionCollection()); 0058 0059 setupGUI(); 0060 0061 setWindowFlags(windowFlags() | Qt::WindowContextHelpButtonHint); 0062 0063 // connections 0064 connect(m_mainWidget, &TimeTrackerWidget::statusBarTextChangeRequested, this, &MainWindow::setStatusBar); 0065 connect(m_mainWidget, &TimeTrackerWidget::currentFileChanged, this, &MainWindow::updateWindowCaptionFile); 0066 connect(m_mainWidget, &TimeTrackerWidget::tasksChanged, this, &MainWindow::updateWindowCaptionTasks); 0067 connect(m_mainWidget, &TimeTrackerWidget::minutesUpdated, this, &MainWindow::updateWindowCaptionTasks); 0068 0069 // Setup context menu request handling 0070 connect(m_mainWidget, 0071 &TimeTrackerWidget::contextMenuRequested, 0072 this, 0073 &MainWindow::taskViewCustomContextMenuRequested); 0074 0075 if (KTimeTrackerSettings::trayIcon()) { 0076 m_tray = new TrayIcon(this); 0077 connect(m_mainWidget, &TimeTrackerWidget::timersActive, m_tray, &TrayIcon::startClock); 0078 connect(m_mainWidget, &TimeTrackerWidget::timersInactive, m_tray, &TrayIcon::stopClock); 0079 connect(m_mainWidget, &TimeTrackerWidget::tasksChanged, m_tray, &TrayIcon::updateToolTip); 0080 } 0081 0082 // Load the specified .ics file in the tasks widget 0083 m_mainWidget->openFile(url); 0084 } 0085 0086 //bool KTimeTrackerPart::openFile() 0087 //{ 0088 // return openFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QStringLiteral("ktimetracker/ktimetracker.ics")); 0089 //} 0090 0091 //bool KTimeTrackerPart::saveFile() 0092 //{ 0093 // m_mainWidget->saveFile(); 0094 // return true; 0095 //} 0096 0097 void MainWindow::setStatusBar(const QString &qs) 0098 { 0099 statusBar()->showMessage(i18n(qs.toUtf8().constData())); 0100 } 0101 0102 MainWindow::~MainWindow() 0103 { 0104 qCDebug(KTT_LOG) << "MainWindow::~MainWindows: Quitting ktimetracker."; 0105 saveGeometry(); 0106 quit(); 0107 } 0108 0109 bool MainWindow::queryClose() 0110 { 0111 auto *app = dynamic_cast<QGuiApplication *>(QGuiApplication::instance()); 0112 if (!m_quitRequested && app && !app->isSavingSession()) { 0113 hide(); 0114 return false; 0115 } 0116 0117 return KMainWindow::queryClose(); 0118 } 0119 0120 void MainWindow::taskViewCustomContextMenuRequested(const QPoint &point) 0121 { 0122 QMenu *pop = dynamic_cast<QMenu *>(guiFactory()->container(QStringLiteral("task_popup"), this)); 0123 if (pop) { 0124 pop->popup(point); 0125 } 0126 } 0127 0128 void MainWindow::quit() 0129 { 0130 if (m_mainWidget->closeAllFiles()) { 0131 m_quitRequested = true; 0132 close(); 0133 } 0134 } 0135 0136 void MainWindow::updateWindowCaptionFile(const QUrl &url) 0137 { 0138 if (!KTimeTrackerSettings::windowTitleCurrentFile()) { 0139 return; 0140 } 0141 0142 this->setCaption(url.toDisplayString(QUrl::PreferLocalFile)); 0143 } 0144 0145 void MainWindow::updateWindowCaptionTasks(const QList<Task *> &activeTasks) 0146 { 0147 if (!KTimeTrackerSettings::windowTitleCurrentTask()) { 0148 return; 0149 } 0150 0151 if (activeTasks.isEmpty()) { 0152 this->setCaption(i18n("No active tasks")); 0153 return; 0154 } 0155 0156 QString qCaption; 0157 0158 // Build the caption with all of the names of the active tasks. 0159 for (int i = 0; i < activeTasks.count(); ++i) { 0160 Task *task = activeTasks.at(i); 0161 if (i > 0) { 0162 qCaption += i18nc("separator between task names", ", "); 0163 } 0164 qCaption += formatTime(task->sessionTime()) + QStringLiteral(" ") + task->name(); 0165 } 0166 0167 this->setCaption(qCaption); 0168 } 0169 0170 #include "moc_mainwindow.cpp"