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 <KNotification> | 31 | #include <KNotification> | ||
29 | #include <KRun> | 32 | #include <KRun> | ||
30 | #include <KWayland/Client/connection_thread.h> | 33 | #include <KWayland/Client/connection_thread.h> | ||
31 | #include <KWayland/Client/plasmashell.h> | 34 | #include <KWayland/Client/plasmashell.h> | ||
Show All 20 Lines | 47 | SpectacleCore::SpectacleCore(StartMode theStartMode, | |||
52 | mStartMode(theStartMode), | 55 | mStartMode(theStartMode), | ||
53 | mNotify(theNotifyOnGrab), | 56 | mNotify(theNotifyOnGrab), | ||
54 | mPlatform(loadPlatform()), | 57 | mPlatform(loadPlatform()), | ||
55 | mMainWindow(nullptr), | 58 | mMainWindow(nullptr), | ||
56 | mIsGuiInited(false), | 59 | mIsGuiInited(false), | ||
57 | mCopyToClipboard(theCopyToClipboard), | 60 | mCopyToClipboard(theCopyToClipboard), | ||
58 | mWaylandPlasmashell(nullptr) | 61 | mWaylandPlasmashell(nullptr) | ||
59 | { | 62 | { | ||
60 | auto lConfig = KSharedConfig::openConfig(QStringLiteral("spectaclerc")); | | |||
61 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | | |||
62 | | ||||
63 | if (!(theSaveFileName.isEmpty() || theSaveFileName.isNull())) { | 63 | if (!(theSaveFileName.isEmpty() || theSaveFileName.isNull())) { | ||
64 | if (QDir::isRelativePath(theSaveFileName)) { | 64 | if (QDir::isRelativePath(theSaveFileName)) { | ||
65 | theSaveFileName = QDir::current().absoluteFilePath(theSaveFileName); | 65 | theSaveFileName = QDir::current().absoluteFilePath(theSaveFileName); | ||
66 | } | 66 | } | ||
67 | setFilename(theSaveFileName); | 67 | setFilename(theSaveFileName); | ||
68 | } | 68 | } | ||
69 | 69 | | |||
70 | // essential connections | 70 | // essential connections | ||
71 | connect(this, &SpectacleCore::errorMessage, this, &SpectacleCore::showErrorMessage); | 71 | connect(this, &SpectacleCore::errorMessage, this, &SpectacleCore::showErrorMessage); | ||
72 | connect(mPlatform.get(), &Platform::newScreenshotTaken, this, &SpectacleCore::screenshotUpdated); | 72 | connect(mPlatform.get(), &Platform::newScreenshotTaken, this, &SpectacleCore::screenshotUpdated); | ||
73 | connect(mPlatform.get(), &Platform::newScreenshotFailed, this, &SpectacleCore::screenshotFailed); | 73 | connect(mPlatform.get(), &Platform::newScreenshotFailed, this, &SpectacleCore::screenshotFailed); | ||
74 | 74 | | |||
75 | auto lImmediateAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate); | 75 | auto lImmediateAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate); | ||
76 | auto lOnClickAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::OnClick); | 76 | auto lOnClickAvailable = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::OnClick); | ||
77 | if ((!lOnClickAvailable) && (theDelayMsec < 0)) { | 77 | if ((!lOnClickAvailable) && (theDelayMsec < 0)) { | ||
78 | theDelayMsec = 0; | 78 | theDelayMsec = 0; | ||
79 | } | 79 | } | ||
80 | 80 | | |||
81 | // reset last region if it should not be remembered across restarts | 81 | // reset last region if it should not be remembered across restarts | ||
82 | auto lSpectacleConfig = SpectacleConfig::instance(); | 82 | if(!Settings::alwaysRememberRegion()) { | ||
83 | if(!lSpectacleConfig->alwaysRememberRegion()) { | 83 | Settings::setCropRegion({0, 0, 0, 0}); | ||
84 | lSpectacleConfig->setCropRegion(QRect()); | | |||
85 | } | 84 | } | ||
86 | 85 | | |||
87 | // set up the export manager | 86 | // set up the export manager | ||
88 | auto lExportManager = ExportManager::instance(); | 87 | auto lExportManager = ExportManager::instance(); | ||
89 | lExportManager->setCaptureMode(theCaptureMode); | 88 | lExportManager->setCaptureMode(theCaptureMode); | ||
90 | connect(lExportManager, &ExportManager::errorMessage, this, &SpectacleCore::showErrorMessage); | 89 | connect(lExportManager, &ExportManager::errorMessage, this, &SpectacleCore::showErrorMessage); | ||
91 | connect(lExportManager, &ExportManager::imageSaved, this, &SpectacleCore::doCopyPath); | 90 | connect(lExportManager, &ExportManager::imageSaved, this, &SpectacleCore::doCopyPath); | ||
92 | connect(lExportManager, &ExportManager::forceNotify, this, &SpectacleCore::doNotify); | 91 | connect(lExportManager, &ExportManager::forceNotify, this, &SpectacleCore::doNotify); | ||
Show All 18 Lines | |||||
111 | } | 110 | } | ||
112 | 111 | | |||
113 | switch (theStartMode) { | 112 | switch (theStartMode) { | ||
114 | case StartMode::DBus: | 113 | case StartMode::DBus: | ||
115 | break; | 114 | break; | ||
116 | case StartMode::Background: { | 115 | case StartMode::Background: { | ||
117 | auto lMsec = (KWindowSystem::compositingActive() ? 200 : 50) + theDelayMsec; | 116 | auto lMsec = (KWindowSystem::compositingActive() ? 200 : 50) + theDelayMsec; | ||
118 | auto lShutterMode = lImmediateAvailable ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | 117 | auto lShutterMode = lImmediateAvailable ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | ||
119 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | 118 | auto lIncludePointer = Settings::includePointer(); | ||
120 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | 119 | auto lIncludeDecorations = Settings::includeDecorations(); | ||
121 | const Platform::GrabMode lCaptureMode = toPlatformGrabMode(theCaptureMode); | 120 | const Platform::GrabMode lCaptureMode = toPlatformGrabMode(theCaptureMode); | ||
122 | QTimer::singleShot(lMsec, this, [ this, lCaptureMode, lShutterMode, lIncludePointer, lIncludeDecorations ]() { | 121 | QTimer::singleShot(lMsec, this, [ this, lCaptureMode, lShutterMode, lIncludePointer, lIncludeDecorations ]() { | ||
123 | mPlatform->doGrab(lShutterMode, lCaptureMode, lIncludePointer, lIncludeDecorations); | 122 | mPlatform->doGrab(lShutterMode, lCaptureMode, lIncludePointer, lIncludeDecorations); | ||
124 | }); | 123 | }); | ||
125 | } | 124 | } | ||
126 | break; | 125 | break; | ||
127 | case StartMode::Gui: | 126 | case StartMode::Gui: | ||
128 | initGui(lGuiConfig.readEntry("includePointer", true), lGuiConfig.readEntry("includeDecorations", true)); | 127 | initGui(Settings::includePointer(), Settings::includeDecorations()); | ||
129 | break; | 128 | break; | ||
130 | } | 129 | } | ||
131 | setUpShortcuts(); | 130 | setUpShortcuts(); | ||
132 | } | 131 | } | ||
133 | 132 | | |||
134 | void SpectacleCore::setUpShortcuts() | 133 | void SpectacleCore::setUpShortcuts() | ||
135 | { | 134 | { | ||
136 | SpectacleConfig* config = SpectacleConfig::instance(); | 135 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->openAction(), Qt::Key_Print); | ||
137 | | ||||
138 | QAction* openAction = config->shortCutActions->action(QStringLiteral("_launch")); | | |||
139 | KGlobalAccel::self()->setGlobalShortcut(openAction, Qt::Key_Print); | | |||
140 | 136 | | |||
141 | QAction* fullScreenAction = config->shortCutActions->action(QStringLiteral("FullScreenScreenShot")); | 137 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->fullScreenAction(), Qt::SHIFT + Qt::Key_Print); | ||
142 | KGlobalAccel::self()->setGlobalShortcut(fullScreenAction, Qt::SHIFT + Qt::Key_Print); | | |||
143 | 138 | | |||
144 | QAction* activeWindowAction = config->shortCutActions->action(QStringLiteral("ActiveWindowScreenShot")); | 139 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->activeWindowAction(), Qt::META + Qt::Key_Print); | ||
145 | KGlobalAccel::self()->setGlobalShortcut(activeWindowAction, Qt::META + Qt::Key_Print); | | |||
146 | 140 | | |||
147 | QAction* regionAction = config->shortCutActions->action(QStringLiteral("RectangularRegionScreenShot")); | 141 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->regionAction(), Qt::META + Qt::SHIFT + Qt::Key_Print); | ||
148 | KGlobalAccel::self()->setGlobalShortcut(regionAction, Qt::META + Qt::SHIFT + Qt::Key_Print); | | |||
149 | 142 | | |||
150 | QAction* currentScreenAction = config->shortCutActions->action(QStringLiteral("CurrentMonitorScreenShot")); | 143 | KGlobalAccel::self()->setGlobalShortcut(ShortcutActions::self()->currentScreenAction(), QList<QKeySequence>()); | ||
151 | KGlobalAccel::self()->setGlobalShortcut(currentScreenAction, QList<QKeySequence>()); | | |||
152 | } | 144 | } | ||
153 | 145 | | |||
154 | QString SpectacleCore::filename() const | 146 | QString SpectacleCore::filename() const | ||
155 | { | 147 | { | ||
156 | return mFileNameString; | 148 | return mFileNameString; | ||
157 | } | 149 | } | ||
158 | 150 | | |||
159 | void SpectacleCore::setFilename(const QString &filename) | 151 | void SpectacleCore::setFilename(const QString &filename) | ||
Show All 12 Lines | 160 | { | |||
172 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | 164 | KConfigGroup lGuiConfig(lConfig, "GuiConfig"); | ||
173 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | 165 | auto lIncludePointer = lGuiConfig.readEntry("includePointer", true); | ||
174 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | 166 | auto lIncludeDecorations = lGuiConfig.readEntry("includeDecorations", true); | ||
175 | 167 | | |||
176 | if (!(mStartMode == StartMode::Gui)) { | 168 | if (!(mStartMode == StartMode::Gui)) { | ||
177 | mStartMode = StartMode::Gui; | 169 | mStartMode = StartMode::Gui; | ||
178 | initGui(lIncludePointer, lIncludeDecorations); | 170 | initGui(lIncludePointer, lIncludeDecorations); | ||
179 | } else { | 171 | } else { | ||
180 | using Actions = SpectacleConfig::PrintKeyActionRunning; | 172 | using Actions = Settings::EnumPrintKeyActionRunning; | ||
181 | switch (SpectacleConfig::instance()->printKeyActionRunning()) { | 173 | switch (Settings::printKeyActionRunning()) { | ||
182 | case Actions::TakeNewScreenshot: { | 174 | case Actions::TakeNewScreenshot: { | ||
183 | auto lShutterMode = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate) ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | 175 | auto lShutterMode = mPlatform->supportedShutterModes().testFlag(Platform::ShutterMode::Immediate) ? Platform::ShutterMode::Immediate : Platform::ShutterMode::OnClick; | ||
184 | auto lGrabMode = toPlatformGrabMode(ExportManager::instance()->captureMode()); | 176 | auto lGrabMode = toPlatformGrabMode(ExportManager::instance()->captureMode()); | ||
185 | QTimer::singleShot(KWindowSystem::compositingActive() ? 200 : 50, this, [this, lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations]() { | 177 | QTimer::singleShot(KWindowSystem::compositingActive() ? 200 : 50, this, [this, lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations]() { | ||
186 | mPlatform->doGrab(lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations); | 178 | mPlatform->doGrab(lShutterMode, lGrabMode, lIncludePointer, lIncludeDecorations); | ||
187 | }); | 179 | }); | ||
188 | break; | 180 | break; | ||
189 | } | 181 | } | ||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Line(s) | 257 | { | |||
280 | if (!mNotify) { | 272 | if (!mNotify) { | ||
281 | emit allDone(); | 273 | emit allDone(); | ||
282 | } | 274 | } | ||
283 | } | 275 | } | ||
284 | break; | 276 | break; | ||
285 | case StartMode::Gui: | 277 | case StartMode::Gui: | ||
286 | mMainWindow->setScreenshotAndShow(thePixmap); | 278 | mMainWindow->setScreenshotAndShow(thePixmap); | ||
287 | 279 | | |||
288 | bool autoSaveImage = SpectacleConfig::instance()->autoSaveImage(); | 280 | bool autoSaveImage = Settings::autoSaveImage(); | ||
289 | bool copyImageToClipboard = SpectacleConfig::instance()->copyImageToClipboard(); | 281 | bool copyImageToClipboard = Settings::copyImageToClipboard(); | ||
290 | 282 | | |||
291 | if (autoSaveImage && copyImageToClipboard) { | 283 | if (autoSaveImage && copyImageToClipboard) { | ||
292 | lExportManager->doSaveAndCopy(); | 284 | lExportManager->doSaveAndCopy(); | ||
293 | } else if (autoSaveImage) { | 285 | } else if (autoSaveImage) { | ||
294 | lExportManager->doSave(); | 286 | lExportManager->doSave(); | ||
295 | } else if (copyImageToClipboard) { | 287 | } else if (copyImageToClipboard) { | ||
296 | lExportManager->doCopyToClipboard(false); | 288 | lExportManager->doCopyToClipboard(false); | ||
297 | } | 289 | } | ||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Line(s) | 315 | { | |||
356 | 348 | | |||
357 | if (!mCopyToClipboard) { | 349 | if (!mCopyToClipboard) { | ||
358 | lNotify->setUrls({theSavedAt}); | 350 | lNotify->setUrls({theSavedAt}); | ||
359 | lNotify->setDefaultAction(i18nc("Open the screenshot we just saved", "Open")); | 351 | lNotify->setDefaultAction(i18nc("Open the screenshot we just saved", "Open")); | ||
360 | connect(lNotify, QOverload<uint>::of(&KNotification::activated), this, [this, theSavedAt](uint index) { | 352 | connect(lNotify, QOverload<uint>::of(&KNotification::activated), this, [this, theSavedAt](uint index) { | ||
361 | if (index == 0) { | 353 | if (index == 0) { | ||
362 | new KRun(theSavedAt, nullptr); | 354 | new KRun(theSavedAt, nullptr); | ||
363 | QTimer::singleShot(250, this, [this] { | 355 | QTimer::singleShot(250, this, [this] { | ||
364 | if (mStartMode != StartMode::Gui || SpectacleConfig::instance()->quitAfterSaveOrCopyChecked()) { | 356 | if (mStartMode != StartMode::Gui || Settings::quitAfterSaveCopyExport()) { | ||
365 | emit allDone(); | 357 | emit allDone(); | ||
366 | } | 358 | } | ||
367 | }); | 359 | }); | ||
368 | } | 360 | } | ||
369 | }); | 361 | }); | ||
370 | } | 362 | } | ||
371 | 363 | | |||
372 | connect(lNotify, &QObject::destroyed, this, [this] { | 364 | connect(lNotify, &QObject::destroyed, this, [this] { | ||
373 | if (mStartMode != StartMode::Gui || SpectacleConfig::instance()->quitAfterSaveOrCopyChecked()) { | 365 | if (mStartMode != StartMode::Gui || Settings::quitAfterSaveCopyExport()) { | ||
374 | emit allDone(); | 366 | emit allDone(); | ||
375 | } | 367 | } | ||
376 | }); | 368 | }); | ||
377 | 369 | | |||
378 | lNotify->sendEvent(); | 370 | lNotify->sendEvent(); | ||
379 | } | 371 | } | ||
380 | 372 | | |||
381 | void SpectacleCore::doCopyPath(const QUrl &savedAt) | 373 | void SpectacleCore::doCopyPath(const QUrl &savedAt) | ||
382 | { | 374 | { | ||
383 | if (SpectacleConfig::instance()->copySaveLocationToClipboard()) { | 375 | if (Settings::copySaveLocation()) { | ||
384 | qApp->clipboard()->setText(savedAt.toLocalFile()); | 376 | qApp->clipboard()->setText(savedAt.toLocalFile()); | ||
385 | } | 377 | } | ||
386 | } | 378 | } | ||
387 | 379 | | |||
388 | void SpectacleCore::doStartDragAndDrop() | 380 | void SpectacleCore::doStartDragAndDrop() | ||
389 | { | 381 | { | ||
390 | auto lExportManager = ExportManager::instance(); | 382 | auto lExportManager = ExportManager::instance(); | ||
391 | QUrl lTempFile = lExportManager->tempSave(); | 383 | QUrl lTempFile = lExportManager->tempSave(); | ||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |