Changeset View
Changeset View
Standalone View
Standalone View
src/SpectacleCore.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2019 David Redondo <kde@david-redondo.de> | ||||
2 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | 3 | * Copyright (C) 2015 Boudhayan Gupta <bgupta@kde.org> | ||
3 | * | 4 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | 5 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU Lesser General Public License as published by | 6 | * it under the terms of the GNU Lesser General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | 7 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | 8 | * (at your option) any later version. | ||
8 | * | 9 | * | ||
9 | * This program is distributed in the hope that it will be useful, | 10 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | 13 | * GNU General Public License for more details. | ||
13 | * | 14 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public License | 15 | * You should have received a copy of the GNU Lesser General Public License | ||
15 | * along with this program; if not, write to the Free Software | 16 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 19 | */ | ||
19 | 20 | | |||
20 | #include "SpectacleCore.h" | 21 | #include "SpectacleCore.h" | ||
21 | #include "spectacle_core_debug.h" | 22 | #include "spectacle_core_debug.h" | ||
22 | 23 | | |||
23 | #include "Config.h" | 24 | #include "Config.h" | ||
25 | #include "settings.h" | ||||
26 | #include "ShortcutActions.h" | ||||
24 | 27 | | |||
25 | #include <KGlobalAccel> | 28 | #include <KGlobalAccel> | ||
26 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
27 | #include <KMessageBox> | 30 | #include <KMessageBox> | ||
28 | #include <KMessageWidget> | 31 | #include <KMessageWidget> | ||
29 | #include <KNotification> | 32 | #include <KNotification> | ||
30 | #include <KRun> | 33 | #include <KRun> | ||
31 | #include <KWindowSystem> | 34 | #include <KWindowSystem> | ||
Show All 17 Lines | 45 | SpectacleCore::SpectacleCore(StartMode theStartMode, | |||
49 | QObject(parent), | 52 | QObject(parent), | ||
50 | mStartMode(theStartMode), | 53 | mStartMode(theStartMode), | ||
51 | mNotify(theNotifyOnGrab), | 54 | mNotify(theNotifyOnGrab), | ||
52 | mPlatform(loadPlatform()), | 55 | mPlatform(loadPlatform()), | ||
53 | mMainWindow(nullptr), | 56 | mMainWindow(nullptr), | ||
54 | mIsGuiInited(false), | 57 | mIsGuiInited(false), | ||
55 | mCopySaveLocationToClipboard(theCopyToClipboard) | 58 | mCopySaveLocationToClipboard(theCopyToClipboard) | ||
56 | { | 59 | { | ||
57 | auto lConfig = KSharedConfig::openConfig(QStringLiteral("spectaclerc")); | | |||
58 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | | |||
59 | | ||||
60 | if (!(theSaveFileName.isEmpty() || theSaveFileName.isNull())) { | 60 | if (!(theSaveFileName.isEmpty() || theSaveFileName.isNull())) { | ||
61 | if (QDir::isRelativePath(theSaveFileName)) { | 61 | if (QDir::isRelativePath(theSaveFileName)) { | ||
62 | theSaveFileName = QDir::current().absoluteFilePath(theSaveFileName); | 62 | theSaveFileName = QDir::current().absoluteFilePath(theSaveFileName); | ||
63 | } | 63 | } | ||
64 | setFilename(theSaveFileName); | 64 | setFilename(theSaveFileName); | ||
65 | } | 65 | } | ||
66 | 66 | | |||
67 | // essential connections | 67 | // essential connections | ||
68 | connect(this, &SpectacleCore::errorMessage, this, &SpectacleCore::showErrorMessage); | 68 | connect(this, &SpectacleCore::errorMessage, this, &SpectacleCore::showErrorMessage); | ||
69 | connect(mPlatform.get(), &Platform::newScreenshotTaken, this, &SpectacleCore::screenshotUpdated); | 69 | connect(mPlatform.get(), &Platform::newScreenshotTaken, this, &SpectacleCore::screenshotUpdated); | ||
70 | connect(mPlatform.get(), &Platform::newScreenshotFailed, this, &SpectacleCore::screenshotFailed); | 70 | connect(mPlatform.get(), &Platform::newScreenshotFailed, this, &SpectacleCore::screenshotFailed); | ||
71 | 71 | | |||
72 | auto lImmediateAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate); | 72 | auto lImmediateAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate); | ||
73 | auto lOnClickAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::OnClick); | 73 | auto lOnClickAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::OnClick); | ||
74 | if ((!lOnClickAvailable) && (theDelayMsec < 0)) { | 74 | if ((!lOnClickAvailable) && (theDelayMsec < 0)) { | ||
75 | theDelayMsec = 0; | 75 | theDelayMsec = 0; | ||
76 | } | 76 | } | ||
77 | 77 | | |||
78 | // reset last region if it should not be remembered across restarts | 78 | // reset last region if it should not be remembered across restarts | ||
79 | auto lSpectacleConfig = SpectacleConfig::instance(); | 79 | if(!Settings::alwaysRememberRegion()) { | ||
80 | if(!lSpectacleConfig->alwaysRememberRegion()) { | 80 | Settings::setCropRegion({0, 0, 0, 0}); | ||
81 | lSpectacleConfig->setCropRegion(QRect()); | | |||
82 | } | 81 | } | ||
83 | 82 | | |||
84 | // set up the export manager | 83 | // set up the export manager | ||
85 | auto lExportManager = ExportManager::instance(); | 84 | auto lExportManager = ExportManager::instance(); | ||
86 | lExportManager->setCaptureMode(theCaptureMode); | 85 | lExportManager->setCaptureMode(theCaptureMode); | ||
87 | connect(lExportManager, &ExportManager::errorMessage, this, &SpectacleCore::showErrorMessage); | 86 | connect(lExportManager, &ExportManager::errorMessage, this, &SpectacleCore::showErrorMessage); | ||
88 | connect(lExportManager, &ExportManager::imageSaved, this, &SpectacleCore::doCopyPath); | 87 | connect(lExportManager, &ExportManager::imageSaved, this, &SpectacleCore::doCopyPath); | ||
89 | connect(lExportManager, &ExportManager::forceNotify, this, &SpectacleCore::doNotify); | 88 | connect(lExportManager, &ExportManager::forceNotify, this, &SpectacleCore::doNotify); | ||
90 | connect(mPlatform.get(), &Platform::windowTitleChanged, lExportManager, &ExportManager::setWindowTitle); | 89 | connect(mPlatform.get(), &Platform::windowTitleChanged, lExportManager, &ExportManager::setWindowTitle); | ||
91 | 90 | | |||
92 | switch (theStartMode) { | 91 | switch (theStartMode) { | ||
93 | case StartMode::DBus: | 92 | case StartMode::DBus: | ||
94 | break; | 93 | break; | ||
95 | case StartMode::Background: { | 94 | case StartMode::Background: { | ||
96 | auto lMsec = (KWindowSystem::compositingActive() ? 200 : 50) + theDelayMsec; | 95 | auto lMsec = (KWindowSystem::compositingActive() ? 200 : 50) + theDelayMsec; | ||
97 | auto lShutterMode = lImmediateAvailable ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | 96 | auto lShutterMode = lImmediateAvailable ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | ||
98 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | 97 | auto lIncludePointer = Settings::includePointer(); | ||
99 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | 98 | auto lIncludeDecorations = Settings::includeDecorations(); | ||
100 | const Platform::GrabMode lCaptureMode = toPlatformGrabMode(theCaptureMode); | 99 | const Platform::GrabMode lCaptureMode = toPlatformGrabMode(theCaptureMode); | ||
101 | QTimer::singleShot(lMsec, this, [ this, lCaptureMode, lShutterMode, lIncludePointer, lIncludeDecorations ]() { | 100 | QTimer::singleShot(lMsec, this, [ this, lCaptureMode, lShutterMode, lIncludePointer, lIncludeDecorations ]() { | ||
102 | mPlatform->doGrab(lShutterMode, lCaptureMode, lIncludePointer, lIncludeDecorations); | 101 | mPlatform->doGrab(lShutterMode, lCaptureMode, lIncludePointer, lIncludeDecorations); | ||
103 | }); | 102 | }); | ||
104 | } | 103 | } | ||
105 | break; | 104 | break; | ||
106 | case StartMode::Gui: | 105 | case StartMode::Gui: | ||
107 | initGui(lGuiConfig.readEntry("includePointer", true), lGuiConfig.readEntry("includeDecorations", true)); | 106 | initGui(Settings::includePointer(), Settings::includeDecorations()); | ||
108 | break; | 107 | break; | ||
109 | } | 108 | } | ||
110 | setUpShortcuts(); | 109 | setUpShortcuts(); | ||
111 | } | 110 | } | ||
112 | 111 | | |||
113 | void SpectacleCore::setUpShortcuts() | 112 | void SpectacleCore::setUpShortcuts() | ||
114 | { | 113 | { | ||
115 | SpectacleConfig* config = SpectacleConfig::instance(); | 114 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->openAction(), Qt::Key_Print); | ||
116 | | ||||
117 | QAction* openAction = config->shortCutActions->action(QStringLiteral("_launch")); | | |||
118 | KGlobalAccel::self()->setGlobalShortcut(openAction, Qt::Key_Print); | | |||
119 | 115 | | |||
120 | QAction* fullScreenAction = config->shortCutActions->action(QStringLiteral("FullScreenScreenShot")); | 116 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->fullScreenAction(), Qt::SHIFT + Qt::Key_Print); | ||
121 | KGlobalAccel::self()->setGlobalShortcut(fullScreenAction, Qt::SHIFT + Qt::Key_Print); | | |||
122 | 117 | | |||
123 | QAction* activeWindowAction = config->shortCutActions->action(QStringLiteral("ActiveWindowScreenShot")); | 118 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->activeWindowAction(), Qt::META + Qt::Key_Print); | ||
124 | KGlobalAccel::self()->setGlobalShortcut(activeWindowAction, Qt::META + Qt::Key_Print); | | |||
125 | 119 | | |||
126 | QAction* regionAction = config->shortCutActions->action(QStringLiteral("RectangularRegionScreenShot")); | 120 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->regionAction(), Qt::META + Qt::SHIFT + Qt::Key_Print); | ||
127 | KGlobalAccel::self()->setGlobalShortcut(regionAction, Qt::META + Qt::SHIFT + Qt::Key_Print); | | |||
128 | 121 | | |||
129 | QAction* currentScreenAction = config->shortCutActions->action(QStringLiteral("CurrentMonitorScreenShot")); | 122 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->currentScreenAction(), QList<QKeySequence>()); | ||
130 | KGlobalAccel::self()->setGlobalShortcut(currentScreenAction, QList<QKeySequence>()); | | |||
131 | } | 123 | } | ||
132 | 124 | | |||
133 | QString SpectacleCore::filename() const | 125 | QString SpectacleCore::filename() const | ||
134 | { | 126 | { | ||
135 | return mFileNameString; | 127 | return mFileNameString; | ||
136 | } | 128 | } | ||
137 | 129 | | |||
138 | void SpectacleCore::setFilename(const QString &filename) | 130 | void SpectacleCore::setFilename(const QString &filename) | ||
Show All 12 Lines | 139 | { | |||
151 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | 143 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | ||
152 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | 144 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | ||
153 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | 145 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | ||
154 | 146 | | |||
155 | if (!(mStartMode == StartMode::Gui)) { | 147 | if (!(mStartMode == StartMode::Gui)) { | ||
156 | mStartMode = StartMode::Gui; | 148 | mStartMode = StartMode::Gui; | ||
157 | initGui(lIncludePointer, lIncludeDecorations); | 149 | initGui(lIncludePointer, lIncludeDecorations); | ||
158 | } else { | 150 | } else { | ||
159 | using Actions = SpectacleConfig::PrintKeyActionRunning; | 151 | using Actions = Settings::EnumPrintKeyActionRunning; | ||
160 | switch (SpectacleConfig::instance()->printKeyActionRunning()) { | 152 | switch (Settings::printKeyActionRunning()) { | ||
161 | case Actions::TakeNewScreenshot: { | 153 | case Actions::TakeNewScreenshot: { | ||
162 | auto lShutterMode = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate) ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | 154 | auto lShutterMode = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate) ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | ||
163 | auto lGrabMode = toPlatformGrabMode(ExportManager::instance()->captureMode()); | 155 | auto lGrabMode = toPlatformGrabMode(ExportManager::instance()->captureMode()); | ||
164 | QTimer::singleShot(KWindowSystem::compositingActive() ? 200 : 50, this, [this, lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations]() { | 156 | QTimer::singleShot(KWindowSystem::compositingActive() ? 200 : 50, this, [this, lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations]() { | ||
165 | mPlatform->doGrab(lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations); | 157 | mPlatform->doGrab(lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations); | ||
166 | }); | 158 | }); | ||
167 | break; | 159 | break; | ||
168 | } | 160 | } | ||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Line(s) | 236 | { | |||
259 | if (!mNotify) { | 251 | if (!mNotify) { | ||
260 | emit allDone(); | 252 | emit allDone(); | ||
261 | } | 253 | } | ||
262 | } | 254 | } | ||
263 | break; | 255 | break; | ||
264 | case StartMode::Gui: | 256 | case StartMode::Gui: | ||
265 | mMainWindow->setScreenshotAndShow(thePixmap); | 257 | mMainWindow->setScreenshotAndShow(thePixmap); | ||
266 | 258 | | |||
267 | bool autoSaveImage = SpectacleConfig::instance()->autoSaveImage(); | 259 | bool autoSaveImage = Settings::autoSaveImage(); | ||
268 | bool copyImageToClipboard = SpectacleConfig::instance()->copyImageToClipboard(); | 260 | bool copyImageToClipboard = Settings::copyImageToClipboard(); | ||
269 | 261 | | |||
270 | if (autoSaveImage && copyImageToClipboard) { | 262 | if (autoSaveImage && copyImageToClipboard) { | ||
271 | lExportManager->doSaveAndCopy(); | 263 | lExportManager->doSaveAndCopy(); | ||
272 | } else if (autoSaveImage) { | 264 | } else if (autoSaveImage) { | ||
273 | lExportManager->doSave(); | 265 | lExportManager->doSave(); | ||
274 | } else if (copyImageToClipboard) { | 266 | } else if (copyImageToClipboard) { | ||
275 | lExportManager->doCopyToClipboard(false); | 267 | lExportManager->doCopyToClipboard(false); | ||
276 | mMainWindow->showInlineMessage(i18n("The screenshot has been copied to the clipboard."), | 268 | mMainWindow->showInlineMessage(i18n("The screenshot has been copied to the clipboard."), | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 330 | if (!mCopySaveLocationToClipboard) { | |||
355 | }); | 347 | }); | ||
356 | } | 348 | } | ||
357 | 349 | | |||
358 | lNotify->sendEvent(); | 350 | lNotify->sendEvent(); | ||
359 | } | 351 | } | ||
360 | 352 | | |||
361 | void SpectacleCore::doCopyPath(const QUrl &savedAt) | 353 | void SpectacleCore::doCopyPath(const QUrl &savedAt) | ||
362 | { | 354 | { | ||
363 | if (SpectacleConfig::instance()->copySaveLocationToClipboard()) { | 355 | if (Settings::copySaveLocation()) { | ||
364 | qApp->clipboard()->setText(savedAt.toLocalFile()); | 356 | qApp->clipboard()->setText(savedAt.toLocalFile()); | ||
365 | } | 357 | } | ||
366 | } | 358 | } | ||
367 | 359 | | |||
368 | void SpectacleCore::doStartDragAndDrop() | 360 | void SpectacleCore::doStartDragAndDrop() | ||
369 | { | 361 | { | ||
370 | auto lExportManager = ExportManager::instance(); | 362 | auto lExportManager = ExportManager::instance(); | ||
371 | QUrl lTempFile = lExportManager->tempSave(); | 363 | QUrl lTempFile = lExportManager->tempSave(); | ||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |