Changeset View
Changeset View
Standalone View
Standalone View
src/Gui/KSMainWindow.cpp
Show All 17 Lines | |||||
18 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
19 | * | 19 | * | ||
20 | * SPDX-License-Identifier: LGPL-2.0-or-later | 20 | * SPDX-License-Identifier: LGPL-2.0-or-later | ||
21 | */ | 21 | */ | ||
22 | 22 | | |||
23 | #include "KSMainWindow.h" | 23 | #include "KSMainWindow.h" | ||
24 | 24 | | |||
25 | #include "Config.h" | 25 | #include "Config.h" | ||
26 | #include "settings.h" | ||||
26 | #include "SettingsDialog/SettingsDialog.h" | 27 | #include "SettingsDialog/SettingsDialog.h" | ||
28 | #include "SettingsDialog/GeneralOptionsPage.h" | ||||
29 | #include "SettingsDialog/SaveOptionsPage.h" | ||||
30 | #include "SettingsDialog/ShortcutsOptionsPage.h" | ||||
27 | 31 | | |||
28 | #include <QApplication> | 32 | #include <QApplication> | ||
29 | #include <QClipboard> | 33 | #include <QClipboard> | ||
30 | #include <QDesktopServices> | 34 | #include <QDesktopServices> | ||
31 | #include <QDBusConnection> | 35 | #include <QDBusConnection> | ||
32 | #include <QDBusMessage> | 36 | #include <QDBusMessage> | ||
33 | #include <QKeyEvent> | 37 | #include <QKeyEvent> | ||
34 | #include <QPrintDialog> | 38 | #include <QPrintDialog> | ||
35 | #include <QPushButton> | 39 | #include <QPushButton> | ||
36 | #include <QTimer> | 40 | #include <QTimer> | ||
37 | #include <QtMath> | 41 | #include <QtMath> | ||
38 | #include <QVariantAnimation> | 42 | #include <QVariantAnimation> | ||
39 | #include <QVBoxLayout> | 43 | #include <QVBoxLayout> | ||
40 | 44 | | |||
41 | #ifdef XCB_FOUND | 45 | #ifdef XCB_FOUND | ||
42 | #include <QX11Info> | 46 | #include <QX11Info> | ||
43 | #include <xcb/xcb.h> | 47 | #include <xcb/xcb.h> | ||
44 | #endif | 48 | #endif | ||
45 | 49 | | |||
46 | #include <KAboutData> | 50 | #include <KAboutData> | ||
47 | #include <KGuiItem> | 51 | #include <KGuiItem> | ||
48 | #include <KHelpMenu> | 52 | #include <KHelpMenu> | ||
49 | #include <KIO/OpenFileManagerWindowJob> | 53 | #include <KIO/OpenFileManagerWindowJob> | ||
54 | #include <KRun> | ||||
50 | #include <KLocalizedString> | 55 | #include <KLocalizedString> | ||
56 | #include <KConfigGroup> | ||||
57 | #include <KSharedConfig> | ||||
51 | #include <KStandardAction> | 58 | #include <KStandardAction> | ||
52 | #include <KWindowSystem> | 59 | #include <KWindowSystem> | ||
53 | 60 | | |||
54 | static const int DEFAULT_WINDOW_HEIGHT = 420; | 61 | static const int DEFAULT_WINDOW_HEIGHT = 420; | ||
55 | static const int DEFAULT_WINDOW_WIDTH = 840; | 62 | static const int DEFAULT_WINDOW_WIDTH = 840; | ||
56 | static const int MAXIMUM_WINDOW_WIDTH = 1000; | 63 | static const int MAXIMUM_WINDOW_WIDTH = 1000; | ||
57 | 64 | | |||
58 | KSMainWindow::KSMainWindow(const Platform::GrabModes &theGrabModes, const Platform::ShutterModes &theShutterModes, QWidget *parent) : | 65 | KSMainWindow::KSMainWindow(const Platform::GrabModes &theGrabModes, const Platform::ShutterModes &theShutterModes, QWidget *parent) : | ||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 123 | { | |||
169 | mDialogButtonBox->addButton(mSaveButton, QDialogButtonBox::ActionRole); | 176 | mDialogButtonBox->addButton(mSaveButton, QDialogButtonBox::ActionRole); | ||
170 | 177 | | |||
171 | // the help menu | 178 | // the help menu | ||
172 | KHelpMenu *helpMenu = new KHelpMenu(this, KAboutData::applicationData(), true); | 179 | KHelpMenu *helpMenu = new KHelpMenu(this, KAboutData::applicationData(), true); | ||
173 | mDialogButtonBox->button(QDialogButtonBox::Help)->setMenu(helpMenu->menu()); | 180 | mDialogButtonBox->button(QDialogButtonBox::Help)->setMenu(helpMenu->menu()); | ||
174 | 181 | | |||
175 | // the tools menu | 182 | // the tools menu | ||
176 | mToolsMenu->addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), | 183 | mToolsMenu->addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), | ||
177 | i18n("Open Screenshots Folder"), | 184 | i18n("Open Default Screenshots Folder"), | ||
178 | this, &KSMainWindow::openScreenshotsFolder); | 185 | this, &KSMainWindow::openScreenshotsFolder); | ||
179 | mToolsMenu->addAction(KStandardAction::print(this, &KSMainWindow::showPrintDialog, this)); | 186 | mToolsMenu->addAction(KStandardAction::print(this, &KSMainWindow::showPrintDialog, this)); | ||
180 | mScreenRecorderToolsMenu = mToolsMenu->addMenu(i18n("Record Screen")); | 187 | mScreenRecorderToolsMenu = mToolsMenu->addMenu(i18n("Record Screen")); | ||
181 | mScreenRecorderToolsMenu->setIcon(QIcon::fromTheme(QStringLiteral("media-record"))); | 188 | mScreenRecorderToolsMenu->setIcon(QIcon::fromTheme(QStringLiteral("media-record"))); | ||
182 | connect(mScreenRecorderToolsMenu, &QMenu::aboutToShow, this, [this]() | 189 | connect(mScreenRecorderToolsMenu, &QMenu::aboutToShow, this, [this]() | ||
183 | { | 190 | { | ||
184 | KMoreToolsMenuFactory *moreToolsMenuFactory = new KMoreToolsMenuFactory(QStringLiteral("spectacle/screenrecorder-tools")); | 191 | KMoreToolsMenuFactory *moreToolsMenuFactory = new KMoreToolsMenuFactory(QStringLiteral("spectacle/screenrecorder-tools")); | ||
185 | moreToolsMenuFactory->setParentWidget(this); | 192 | moreToolsMenuFactory->setParentWidget(this); | ||
Show All 37 Lines | |||||
223 | 230 | | |||
224 | // Allow Ctrl+Q to quit the app | 231 | // Allow Ctrl+Q to quit the app | ||
225 | QAction *actionQuit = KStandardAction::quit(qApp, &QApplication::quit, this); | 232 | QAction *actionQuit = KStandardAction::quit(qApp, &QApplication::quit, this); | ||
226 | actionQuit->setShortcut(QKeySequence::Quit); | 233 | actionQuit->setShortcut(QKeySequence::Quit); | ||
227 | addAction(actionQuit); | 234 | addAction(actionQuit); | ||
228 | 235 | | |||
229 | // message: open containing folder | 236 | // message: open containing folder | ||
230 | mOpenContaining = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"), mMessageWidget); | 237 | mOpenContaining = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"), mMessageWidget); | ||
231 | connect(mOpenContaining, &QAction::triggered, [=] { KIO::highlightInFileManager({SpectacleConfig::instance()->lastSaveFile()});}); | 238 | connect(mOpenContaining, &QAction::triggered, [=] { KIO::highlightInFileManager({Settings::lastSaveLocation()});}); | ||
232 | 239 | | |||
233 | mHideMessageWidgetTimer = new QTimer(this); | 240 | mHideMessageWidgetTimer = new QTimer(this); | ||
234 | connect(mHideMessageWidgetTimer, &QTimer::timeout, | 241 | // connect(mHideMessageWidgetTimer, &QTimer::timeout, | ||
235 | mMessageWidget, &KMessageWidget::animatedHide); | 242 | // mMessageWidget, &KMessageWidget::animatedHide); | ||
236 | mHideMessageWidgetTimer->setInterval(10000); | 243 | mHideMessageWidgetTimer->setInterval(10000); | ||
237 | // done with the init | 244 | // done with the init | ||
238 | } | 245 | } | ||
239 | 246 | | |||
240 | int KSMainWindow::windowWidth(const QPixmap &pixmap) const | 247 | int KSMainWindow::windowWidth(const QPixmap &pixmap) const | ||
241 | { | 248 | { | ||
242 | // Calculates what the width of the window should be for the captured image to perfectly fit | 249 | // Calculates what the width of the window should be for the captured image to perfectly fit | ||
243 | // the area reserved for the image, with the height already set. | 250 | // the area reserved for the image, with the height already set. | ||
244 | 251 | | |||
245 | const float pixmapAspectRatio = (float)pixmap.width() / pixmap.height(); | 252 | const float pixmapAspectRatio = (float)pixmap.width() / pixmap.height(); | ||
246 | const int imageHeight = mKSWidget->height() - 2 * layout()->spacing(); | 253 | const int imageHeight = mKSWidget->height() - 2 * layout()->spacing(); | ||
247 | const int imageWidth = pixmapAspectRatio * imageHeight; | 254 | const int imageWidth = pixmapAspectRatio * imageHeight; | ||
248 | 255 | | |||
249 | int alignedWindowWidth = qMin(mKSWidget->imagePaddingWidth() + imageWidth, MAXIMUM_WINDOW_WIDTH); | 256 | int alignedWindowWidth = qMin(mKSWidget->imagePaddingWidth() + imageWidth, MAXIMUM_WINDOW_WIDTH); | ||
250 | alignedWindowWidth += layout()->contentsMargins().left() + layout()->contentsMargins().right(); | 257 | alignedWindowWidth += layout()->contentsMargins().left() + layout()->contentsMargins().right(); | ||
251 | alignedWindowWidth += 2; // margins is removing 1 - 1 pixel for some reason | 258 | alignedWindowWidth += 2; // margins is removing 1 - 1 pixel for some reason | ||
252 | | ||||
253 | return alignedWindowWidth; | 259 | return alignedWindowWidth; | ||
254 | } | 260 | } | ||
255 | 261 | | |||
256 | void KSMainWindow::setDefaultSaveAction() | 262 | void KSMainWindow::setDefaultSaveAction() | ||
257 | { | 263 | { | ||
258 | switch (SpectacleConfig::instance()->lastUsedSaveMode()) { | 264 | switch (Settings::lastUsedSaveMode()) { | ||
ervin: Smells like you could have used an enum for that one. | |||||
259 | case SaveMode::SaveAs: | 265 | case Settings::SaveAs: | ||
260 | default: | | |||
261 | mSaveButton->setDefaultAction(mSaveAsAction); | 266 | mSaveButton->setDefaultAction(mSaveAsAction); | ||
262 | mSaveButton->setText(i18n("Save As...")); | 267 | mSaveButton->setText(i18n("Save As...")); | ||
263 | break; | 268 | break; | ||
264 | case SaveMode::Save: | 269 | case Settings::Save: | ||
265 | mSaveButton->setDefaultAction(mSaveAction); | 270 | mSaveButton->setDefaultAction(mSaveAction); | ||
266 | break; | 271 | break; | ||
267 | } | 272 | } | ||
268 | } | 273 | } | ||
269 | 274 | | |||
270 | // overrides | 275 | // overrides | ||
271 | 276 | | |||
272 | void KSMainWindow::moveEvent(QMoveEvent *event) | 277 | void KSMainWindow::moveEvent(QMoveEvent *event) | ||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 366 | if (printDialog.exec() == QDialog::Accepted) { | |||
362 | ExportManager::instance()->doPrint(printer); | 367 | ExportManager::instance()->doPrint(printer); | ||
363 | return; | 368 | return; | ||
364 | } | 369 | } | ||
365 | delete printer; | 370 | delete printer; | ||
366 | } | 371 | } | ||
367 | 372 | | |||
368 | void KSMainWindow::openScreenshotsFolder() | 373 | void KSMainWindow::openScreenshotsFolder() | ||
369 | { | 374 | { | ||
370 | // Highlight last screenshot in file manager if user saved at least once ever | 375 | new KRun(Settings::defaultSaveLocation(), this); | ||
371 | // (since last save and saveas file names are stored in spectaclerc), otherwise, | | |||
372 | // if in save mode, open default save location from configure > save > location | | |||
373 | // if in save as mode, open last save as files location | | |||
374 | // failsafe for either option is default save location from configure > save > location | | |||
375 | SpectacleConfig *cfgManager = SpectacleConfig::instance(); | | |||
376 | ExportManager *exportManager = ExportManager::instance(); | | |||
377 | QUrl location; | | |||
378 | | ||||
379 | switch(cfgManager->lastUsedSaveMode()) { | | |||
380 | case SaveMode::Save: | | |||
381 | location = cfgManager->lastSaveFile(); | | |||
382 | if (!exportManager->isFileExists(location)) { | | |||
383 | location = cfgManager->defaultSaveLocation(); | | |||
384 | } | | |||
385 | break; | | |||
386 | case SaveMode::SaveAs: | | |||
387 | location = cfgManager->lastSaveAsFile(); // already has a "/" at the end | | |||
388 | if (!exportManager->isFileExists(location)) { | | |||
389 | location = cfgManager->lastSaveAsLocation(); | | |||
390 | } | | |||
391 | break; | | |||
392 | } | | |||
393 | | ||||
394 | KIO::highlightInFileManager({location}); | | |||
395 | } | 376 | } | ||
396 | 377 | | |||
397 | void KSMainWindow::quit(const QuitBehavior quitBehavior) | 378 | void KSMainWindow::quit(const QuitBehavior quitBehavior) | ||
398 | { | 379 | { | ||
399 | qApp->setQuitOnLastWindowClosed(false); | 380 | qApp->setQuitOnLastWindowClosed(false); | ||
400 | hide(); | 381 | hide(); | ||
401 | 382 | | |||
402 | if (quitBehavior == QuitBehavior::QuitImmediately) { | 383 | if (quitBehavior == QuitBehavior::QuitImmediately) { | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 439 | showInlineMessage(i18n("The shared image link (<a href=\"%1\">%1</a>) has been copied to the clipboard.", message), | |||
459 | KMessageWidget::Positive, MessageDuration::Persistent); | 440 | KMessageWidget::Positive, MessageDuration::Persistent); | ||
460 | QApplication::clipboard()->setText(message); | 441 | QApplication::clipboard()->setText(message); | ||
461 | } | 442 | } | ||
462 | } | 443 | } | ||
463 | } | 444 | } | ||
464 | 445 | | |||
465 | void KSMainWindow::imageCopied() | 446 | void KSMainWindow::imageCopied() | ||
466 | { | 447 | { | ||
467 | SpectacleConfig::instance()->quitAfterSaveOrCopyChecked() | 448 | Settings::quitAfterSaveCopyExport() | ||
468 | ? quit() | 449 | ? quit() | ||
469 | : showInlineMessage(i18n("The screenshot has been copied to the clipboard."), | 450 | : showInlineMessage(i18n("The screenshot has been copied to the clipboard."), | ||
470 | KMessageWidget::Information); | 451 | KMessageWidget::Information); | ||
471 | } | 452 | } | ||
472 | 453 | | |||
473 | void KSMainWindow::showPreferencesDialog() | 454 | void KSMainWindow::showPreferencesDialog() | ||
474 | { | 455 | { | ||
475 | SettingsDialog prefDialog(this); | 456 | if (KConfigDialog::showDialog(QStringLiteral("settings"))) { | ||
476 | prefDialog.exec(); | 457 | return; | ||
458 | } | ||||
459 | (new SettingsDialog(this))->show(); | ||||
Style related, consider putting return statement in a new line. crossi: Style related, consider putting return statement in a new line.
Also, KDE code style requires… | |||||
477 | } | 460 | } | ||
478 | 461 | | |||
479 | void KSMainWindow::imageSaved(const QUrl &location) | 462 | void KSMainWindow::imageSaved(const QUrl &location) | ||
480 | { | 463 | { | ||
481 | setWindowTitle(location.fileName()); | 464 | setWindowTitle(location.fileName()); | ||
482 | setWindowModified(false); | 465 | setWindowModified(false); | ||
483 | showInlineMessage(i18n("The screenshot was saved as <a href=\"%1\">%2</a>", | 466 | showInlineMessage(i18n("The screenshot was saved as <a href=\"%1\">%2</a>", | ||
484 | location.toString(), location.fileName()), KMessageWidget::Positive, | 467 | location.toString(), location.fileName()), KMessageWidget::Positive, | ||
485 | MessageDuration::AutoHide, {mOpenContaining}); | 468 | MessageDuration::AutoHide, {mOpenContaining}); | ||
486 | } | 469 | } | ||
487 | 470 | | |||
488 | void KSMainWindow::imageSavedAndCopied(const QUrl &location) | 471 | void KSMainWindow::imageSavedAndCopied(const QUrl &location) | ||
489 | { | 472 | { | ||
490 | setWindowTitle(location.fileName()); | 473 | setWindowTitle(location.fileName()); | ||
491 | setWindowModified(false); | 474 | setWindowModified(false); | ||
492 | showInlineMessage(i18n("The screenshot was copied to the clipboard and saved as <a href=\"%1\">%2</a>", | 475 | showInlineMessage(i18n("The screenshot was copied to the clipboard and saved as <a href=\"%1\">%2</a>", | ||
493 | location.toString(), location.fileName()), KMessageWidget::Positive, | 476 | location.toString(), location.fileName()), KMessageWidget::Positive, | ||
494 | MessageDuration::AutoHide, {mOpenContaining}); | 477 | MessageDuration::AutoHide, {mOpenContaining}); | ||
495 | } | 478 | } | ||
496 | 479 | | |||
497 | void KSMainWindow::save() | 480 | void KSMainWindow::save() | ||
498 | { | 481 | { | ||
499 | SpectacleConfig::instance()->setLastUsedSaveMode(SaveMode::Save); | 482 | Settings::setLastUsedSaveMode(Settings::Save); | ||
500 | setDefaultSaveAction(); | 483 | setDefaultSaveAction(); | ||
501 | 484 | | |||
502 | const bool quitChecked = SpectacleConfig::instance()->quitAfterSaveOrCopyChecked(); | 485 | const bool quitChecked = Settings::quitAfterSaveCopyExport(); | ||
503 | ExportManager::instance()->doSave(QUrl(), /* notify */ quitChecked); | 486 | ExportManager::instance()->doSave(QUrl(), /* notify */ quitChecked); | ||
504 | if (quitChecked) { | 487 | if (quitChecked) { | ||
505 | quit(QuitBehavior::QuitExternally); | 488 | quit(QuitBehavior::QuitExternally); | ||
506 | } | 489 | } | ||
507 | } | 490 | } | ||
508 | 491 | | |||
509 | void KSMainWindow::saveAs() | 492 | void KSMainWindow::saveAs() | ||
510 | { | 493 | { | ||
511 | SpectacleConfig::instance()->setLastUsedSaveMode(SaveMode::SaveAs); | 494 | Settings::setLastUsedSaveMode(Settings::SaveAs); | ||
512 | setDefaultSaveAction(); | 495 | setDefaultSaveAction(); | ||
513 | 496 | | |||
514 | const bool quitChecked = SpectacleConfig::instance()->quitAfterSaveOrCopyChecked(); | 497 | const bool quitChecked = Settings::quitAfterSaveCopyExport(); | ||
515 | if (ExportManager::instance()->doSaveAs(this, /* notify */ quitChecked) && quitChecked) { | 498 | if (ExportManager::instance()->doSaveAs(this, /* notify */ quitChecked) && quitChecked) { | ||
516 | quit(QuitBehavior::QuitExternally); | 499 | quit(QuitBehavior::QuitExternally); | ||
517 | } | 500 | } | ||
518 | } | 501 | } | ||
519 | 502 | | |||
520 | void KSMainWindow::restoreWindowTitle() | 503 | void KSMainWindow::restoreWindowTitle() | ||
521 | { | 504 | { | ||
522 | if (isWindowModified()) { | 505 | if (isWindowModified()) { | ||
523 | setWindowTitle(i18nc("@title:window Unsaved Screenshot", "Unsaved[*]")); | 506 | setWindowTitle(i18nc("@title:window Unsaved Screenshot", "Unsaved[*]")); | ||
524 | } else { | 507 | } else { | ||
525 | setWindowTitle(SpectacleConfig::instance()->lastSaveFile().fileName()); | 508 | setWindowTitle(Settings::lastSaveLocation().fileName()); | ||
526 | } | 509 | } | ||
527 | } | 510 | } | ||
528 | 511 | | |||
529 | /* This event handler enables all Buttons to be activated with Enter. Normally only QPushButton can | 512 | /* This event handler enables all Buttons to be activated with Enter. Normally only QPushButton can | ||
530 | * be activated with Enter but we also use QToolButtons so we handle the event ourselves */ | 513 | * be activated with Enter but we also use QToolButtons so we handle the event ourselves */ | ||
531 | void KSMainWindow::keyPressEvent(QKeyEvent* event) | 514 | void KSMainWindow::keyPressEvent(QKeyEvent* event) | ||
532 | { | 515 | { | ||
533 | if (event->key() == Qt::Key_Return) { | 516 | if (event->key() == Qt::Key_Return) { | ||
Show All 14 Lines |
Smells like you could have used an enum for that one.