Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
88 KB
Referenced Files
None
Subscribers
None
diff --git a/dialogs/previewdialog.cpp b/dialogs/previewdialog.cpp
index 16ce3d6..e21aa9f 100644
--- a/dialogs/previewdialog.cpp
+++ b/dialogs/previewdialog.cpp
@@ -1,451 +1,451 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "previewdialog.h"
#include "../tools/screenshot.h"
#include "../tools/screenshotmanager.h"
#include "../tools/os.h"
#include <QApplication>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QDrag>
#include <QGraphicsDropShadowEffect>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QList>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
#include <QObject>
#include <QPalette>
#include <QPushButton>
#include <QSettings>
#include <QStackedLayout>
#include <QToolButton>
#include <QUrl>
PreviewDialog::PreviewDialog(QWidget *parent) :
QDialog(parent), mAutoclose(0), mAutocloseAction(0), mAutocloseReset(0), mPosition(0), mSize(0)
{
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowTitle(tr("Screenshot Preview"));
setContextMenuPolicy(Qt::CustomContextMenu);
auto settings = ScreenshotManager::instance()->settings();
mSize = settings->value("options/previewSize", 300).toInt();
mPosition = settings->value("options/previewPosition", 3).toInt();
if (settings->value("options/previewAutoclose", false).toBool()) {
mAutoclose = settings->value("options/previewAutocloseTime").toInt();
mAutocloseReset = mAutoclose;
mAutocloseAction = settings->value("options/previewAutocloseAction").toInt();
}
auto layout = new QHBoxLayout;
mStack = new QStackedLayout;
connect(mStack, &QStackedLayout::currentChanged, this, &PreviewDialog::indexChanged);
mPrevButton = new QPushButton(os::icon("arrow-left"), "", this);
connect(mPrevButton, &QPushButton::clicked, this, &PreviewDialog::previous);
mNextButton = new QPushButton(os::icon("arrow-right"), "", this);
connect(mNextButton, &QPushButton::clicked, this, &PreviewDialog::next);
mPrevButton->setCursor(Qt::PointingHandCursor);
mPrevButton->setFlat(true);
mPrevButton->setGraphicsEffect(os::shadow(Qt::white));
mPrevButton->setIconSize(QSize(24, 24));
mPrevButton->setVisible(false);
mNextButton->setCursor(Qt::PointingHandCursor);
mNextButton->setFlat(true);
mNextButton->setGraphicsEffect(os::shadow(Qt::white));
mNextButton->setIconSize(QSize(24, 24));
mNextButton->setVisible(false);
layout->addWidget(mPrevButton);
layout->addLayout(mStack);
layout->addWidget(mNextButton);
layout->setMargin(0);
layout->setContentsMargins(6, 6, 6, 6);
mStack->setMargin(0);
setMaximumHeight(mSize);
setLayout(layout);
if (mAutoclose) {
startTimer(1000);
}
auto contextMenu = new QMenu(this);
contextMenu->setTitle(tr("Global Preview Actions"));
contextMenu->addAction(os::icon("yes") , tr("Save All") , this, &PreviewDialog::acceptAll);
contextMenu->addAction(os::icon("imgur"), tr("Upload All"), this, &PreviewDialog::uploadAll);
contextMenu->addSeparator();
contextMenu->addAction(os::icon("no") , tr("Cancel All"), this, &PreviewDialog::rejectAll);
connect(this, &PreviewDialog::customContextMenuRequested, contextMenu, [contextMenu](const QPoint &pos) {
Q_UNUSED(pos)
contextMenu->popup(QCursor::pos());
});
}
void PreviewDialog::add(Screenshot *screenshot)
{
if (!isVisible()) {
show();
}
if (mAutoclose) {
mAutoclose = mAutocloseReset;
}
QLabel *label = new QLabel(this);
label->setGraphicsEffect(os::shadow());
bool smallShot = false;
QSize size = screenshot->pixmap().size();
if (size.width() > mSize || size.height() > mSize) {
size.scale(mSize, mSize, Qt::KeepAspectRatio);
} else {
smallShot = true;
}
QPixmap thumbnail = screenshot->pixmap().scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
label->setPixmap(thumbnail);
thumbnail = QPixmap();
label->setAlignment(Qt::AlignCenter);
if (size.height() < 120) {
label->setMinimumHeight(120);
}
if (size.width() < 140) {
label->setMinimumWidth(140);
}
label->resize(size);
QPushButton *discardPushButton = new QPushButton(os::icon("no") , "", label);
QPushButton *enlargePushButton = new QPushButton(os::icon("preview"), "", label);
QToolButton *confirmPushButton = new QToolButton(label);
confirmPushButton->setIconSize(QSize(24, 24));
confirmPushButton->setCursor(Qt::PointingHandCursor);
confirmPushButton->setGraphicsEffect(os::shadow(Qt::white));
if (ScreenshotManager::instance()->settings()->value("options/previewDefaultAction", 0).toInt() == 0
|| ScreenshotManager::instance()->settings()->value("options/uploadAuto").toBool()) {
// Default button, confirm & upload.
confirmPushButton->setIcon(os::icon("yes"));
if (!ScreenshotManager::instance()->settings()->value("options/uploadAuto").toBool()) {
QMenu *confirmMenu = new QMenu(confirmPushButton);
confirmMenu->setObjectName("confirmMenu");
QAction *uploadAction = new QAction(os::icon("imgur"), tr("Upload"), confirmPushButton);
connect(uploadAction, &QAction::triggered, screenshot, &Screenshot::markUpload);
- connect(uploadAction, &QAction::triggered, screenshot, &Screenshot::confirm);
+ connect(uploadAction, &QAction::triggered, screenshot, [screenshot] { screenshot->confirm(true); });
connect(uploadAction, &QAction::triggered, this, &PreviewDialog::closePreview);
connect(this, &PreviewDialog::uploadAll, uploadAction, &QAction::trigger);
confirmMenu->addAction(uploadAction);
confirmPushButton->setMenu(confirmMenu);
confirmPushButton->setPopupMode(QToolButton::MenuButtonPopup);
}
connect(this, &PreviewDialog::acceptAll, confirmPushButton, &QPushButton::click);
- connect(confirmPushButton, &QPushButton::clicked, screenshot, &Screenshot::confirm);
+ connect(confirmPushButton, &QPushButton::clicked, screenshot, [screenshot] { screenshot->confirm(true); });
connect(confirmPushButton, &QPushButton::clicked, this, &PreviewDialog::closePreview);
} else {
// Reversed button, upload & confirm.
confirmPushButton->setIcon(os::icon("imgur"));
QMenu *confirmMenu = new QMenu(confirmPushButton);
confirmMenu->setObjectName("confirmMenu");
QAction *confirmAction = new QAction(os::icon("yes"), tr("Save"), confirmPushButton);
connect(this, &PreviewDialog::acceptAll, confirmAction, &QAction::trigger);
- connect(confirmAction, &QAction::triggered, screenshot, &Screenshot::confirm);
+ connect(confirmAction, &QAction::triggered, screenshot, [screenshot] { screenshot->confirm(true); });
connect(confirmAction, &QAction::triggered, this, &PreviewDialog::closePreview);
connect(confirmPushButton, &QPushButton::clicked, screenshot, &Screenshot::markUpload);
- connect(confirmPushButton, &QPushButton::clicked, screenshot, &Screenshot::confirm);
+ connect(confirmPushButton, &QPushButton::clicked, screenshot, [screenshot] { screenshot->confirm(true); });
connect(confirmPushButton, &QPushButton::clicked, this, &PreviewDialog::closePreview);
connect(this, &PreviewDialog::uploadAll, confirmPushButton, &QPushButton::click);
confirmMenu->addAction(confirmAction);
confirmPushButton->setMenu(confirmMenu);
confirmPushButton->setPopupMode(QToolButton::MenuButtonPopup);
}
confirmPushButton->setAutoRaise(true);
confirmPushButton->setVisible(false);
discardPushButton->setIconSize(QSize(24, 24));
discardPushButton->setCursor(Qt::PointingHandCursor);
discardPushButton->setGraphicsEffect(os::shadow(Qt::white));
discardPushButton->setFlat(true);
discardPushButton->setVisible(false);
enlargePushButton->setIconSize(QSize(22, 22));
enlargePushButton->setCursor(Qt::PointingHandCursor);
enlargePushButton->setGraphicsEffect(os::shadow(Qt::white));
enlargePushButton->setFlat(true);
enlargePushButton->setVisible(false);
enlargePushButton->setDisabled(smallShot);
connect(this, &PreviewDialog::rejectAll, discardPushButton, &QPushButton::click);
connect(discardPushButton, &QPushButton::clicked, screenshot, &Screenshot::discard);
connect(discardPushButton, &QPushButton::clicked, this, &PreviewDialog::closePreview);
connect(enlargePushButton, &QPushButton::clicked, this, &PreviewDialog::enlargePreview);
QHBoxLayout *wlayout = new QHBoxLayout;
wlayout->addWidget(confirmPushButton);
wlayout->addStretch();
wlayout->addWidget(enlargePushButton);
wlayout->addStretch();
wlayout->addWidget(discardPushButton);
wlayout->setMargin(0);
QVBoxLayout *wl = new QVBoxLayout;
wl->addStretch();
wl->addLayout(wlayout);
wl->setMargin(0);
label->setLayout(wl);
label->setProperty("screenshotObject", QVariant::fromValue<Screenshot *>(screenshot));
mStack->addWidget(label);
mStack->setCurrentIndex(mStack->count() - 1);
mNextButton->setEnabled(false);
if (mStack->count() >= 2 && !mNextButton->isVisible()) {
mNextButton->setVisible(true);
mPrevButton->setVisible(true);
}
relocate();
}
int PreviewDialog::count() const
{
return mStack->count();
}
//
void PreviewDialog::closePreview()
{
QWidget *widget = mStack->currentWidget();
mStack->removeWidget(widget);
widget->deleteLater();
if (mStack->count() == 0) {
close();
} else {
relocate();
}
}
void PreviewDialog::enlargePreview()
{
Screenshot *screenshot = mStack->currentWidget()->property("screenshotObject").value<Screenshot *>();
if (screenshot) {
QFileInfo info(screenshot->unloadedFileName());
QDesktopServices::openUrl(QUrl(info.absoluteFilePath()));
}
}
void PreviewDialog::indexChanged(int i)
{
if (i == mStack->count() - 1) {
mNextButton->setEnabled(false);
mPrevButton->setEnabled(true);
}
if (i == 0 && mStack->count() > 1) {
mNextButton->setEnabled(true);
mPrevButton->setEnabled(false);
}
if (i != 0 && i != mStack->count() - 1) {
mNextButton->setEnabled(true);
mPrevButton->setEnabled(true);
}
if (mStack->count() < 2) {
mNextButton->setEnabled(false);
mPrevButton->setEnabled(false);
}
if (mStack->widget(i)) {
mStack->widget(i)->setFocus();
}
if (mStack->count() > 1) {
setWindowTitle(tr("Screenshot Preview (%1 of %2)").arg(mStack->currentIndex() + 1).arg(mStack->count()));
} else {
setWindowTitle(tr("Screenshot Preview"));
}
}
void PreviewDialog::next()
{
mStack->setCurrentIndex(mStack->currentIndex() + 1);
relocate();
}
void PreviewDialog::previous()
{
mStack->setCurrentIndex(mStack->currentIndex() - 1);
relocate();
}
void PreviewDialog::relocate()
{
updateGeometry();
resize(minimumSizeHint());
QApplication::sendEvent(this, new QEvent(QEvent::Enter)); // Ensures the buttons are visible.
QPoint where;
switch (mPosition) {
case 0:
where = QApplication::desktop()->availableGeometry(this).topLeft();
break;
case 1:
where = QApplication::desktop()->availableGeometry(this).topRight();
where.setX(where.x() - frameGeometry().width());
break;
case 2:
where = QApplication::desktop()->availableGeometry(this).bottomLeft();
where.setY(where.y() - frameGeometry().height());
break;
case 3:
default:
where = QApplication::desktop()->availableGeometry(this).bottomRight();
where.setX(where.x() - frameGeometry().width());
where.setY(where.y() - frameGeometry().height());
break;
}
move(where);
}
//
bool PreviewDialog::event(QEvent *event)
{
if ((event->type() == QEvent::Enter || event->type() == QEvent::Leave)
&& mStack->currentWidget()) {
for (QObject *child : mStack->currentWidget()->children()) {
QWidget *widget = qobject_cast<QWidget *>(child);
if (widget) {
// Lets avoid disappearing buttons and bail if the menu is open.
QMenu *confirmMenu = widget->findChild<QMenu *>("confirmMenu");
if (confirmMenu && confirmMenu->isVisible()) {
return false;
}
widget->setVisible((event->type() == QEvent::Enter));
}
}
} else if (event->type() == QEvent::Close) {
if (mStack->count() != 0) {
emit rejectAll();
}
deleteLater();
}
return QDialog::event(event);
}
void PreviewDialog::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event);
enlargePreview();
}
void PreviewDialog::mousePressEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - mDragStartPosition).manhattanLength()
< QApplication::startDragDistance())
return;
Screenshot *screenshot = mStack->currentWidget()->property("screenshotObject").value<Screenshot *>();
if (screenshot) {
QFileInfo info(screenshot->unloadedFileName());
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
mimeData->setUrls(QList<QUrl>() << QUrl::fromLocalFile(info.absoluteFilePath()));
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}
void PreviewDialog::mouseMoveEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
mDragStartPosition = event->pos();
}
}
void PreviewDialog::timerEvent(QTimerEvent *event)
{
if (mAutoclose == 0) {
if (mAutocloseAction == 0) {
emit acceptAll();
} else if (mAutocloseAction == 1) {
emit uploadAll();
} else {
emit rejectAll();
}
} else if (mAutoclose < 0) {
killTimer(event->timerId());
} else {
setWindowTitle(tr("Preview: Closing in %1").arg(mAutoclose));
mAutoclose--;
}
}
diff --git a/dialogs/updaterdialog.cpp b/dialogs/updaterdialog.cpp
index d2ccae7..6b5167f 100644
--- a/dialogs/updaterdialog.cpp
+++ b/dialogs/updaterdialog.cpp
@@ -1,64 +1,64 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QApplication>
#include <QDesktopServices>
#include <QLabel>
#include <QLayout>
#include <QProgressBar>
#include <QUrl>
#include "updaterdialog.h"
#include "../tools/os.h"
UpdaterDialog::UpdaterDialog(QWidget *parent) :
QProgressDialog("", tr("Cancel"), 0, 0, parent)
{
setWindowTitle(tr("Updater - Lightscreen"));
setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
setAutoClose(false);
QProgressBar *bar = new QProgressBar(this);
bar->setTextVisible(false);
bar->setRange(0, 0);
QLabel *label = new QLabel(tr("Checking for updates..."), this);
- connect(label, SIGNAL(linkActivated(QString)), this, SLOT(link(QString)));
+ connect(label, &QLabel::linkActivated, this, &UpdaterDialog::link);
setLabel(label);
setBar(bar);
}
void UpdaterDialog::updateDone(bool result)
{
if (result) {
setLabelText(tr("There's a new version available,<br> please see <a href=\"https://lightscreen.com.ar/whatsnew/%1\">the Lighscreen website</a>.").arg(qApp->applicationVersion()));
} else {
setLabelText(tr("No new versions available."));
}
setMaximum(1);
setCancelButtonText(tr("Close"));
}
void UpdaterDialog::link(QString url)
{
QDesktopServices::openUrl(url);
}
diff --git a/lightscreenwindow.cpp b/lightscreenwindow.cpp
index 460556e..4d393bf 100644
--- a/lightscreenwindow.cpp
+++ b/lightscreenwindow.cpp
@@ -1,1027 +1,1027 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QDate>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QFileInfo>
#include <QKeyEvent>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QPointer>
#include <QProcess>
#include <QSettings>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QToolTip>
#include <QUrl>
#include <QSound>
#include <QKeyEvent>
#ifdef Q_OS_WIN
#include <windows.h>
#include <QtWinExtras>
#endif
//
//Lightscreen includes
//
#include "lightscreenwindow.h"
#include "dialogs/optionsdialog.h"
#include "dialogs/previewdialog.h"
#include "dialogs/historydialog.h"
#include "tools/os.h"
#include "tools/screenshot.h"
#include "tools/screenshotmanager.h"
#include "tools/UGlobalHotkey/uglobalhotkeys.h"
#include "tools/uploader/uploader.h"
#include "updater/updater.h"
LightscreenWindow::LightscreenWindow(QWidget *parent) :
QMainWindow(parent),
mDoCache(false),
mHideTrigger(false),
mReviveMain(false),
mWasVisible(true),
mLastMessage(0),
mLastMode(Screenshot::None),
mLastScreenshot(),
mHasTaskbarButton(false)
{
ui.setupUi(this);
ui.screenPushButton->setIcon(os::icon("screen.big"));
ui.areaPushButton->setIcon(os::icon("area.big"));
ui.windowPushButton->setIcon(os::icon("pickWindow.big"));
ui.optionsPushButton->setIcon(os::icon("configure"));
ui.folderPushButton->setIcon(os::icon("folder"));
ui.imgurPushButton->setIcon(os::icon("imgur"));
createUploadMenu();
#ifdef Q_OS_WIN
if (QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS7) {
mTaskbarButton = new QWinTaskbarButton(this);
mHasTaskbarButton = true;
if (QtWin::isCompositionEnabled()) {
setAttribute(Qt::WA_NoSystemBackground);
QtWin::enableBlurBehindWindow(this);
QtWin::extendFrameIntoClientArea(this, QMargins(-1, -1, -1, -1));
}
}
if (QSysInfo::WindowsVersion < QSysInfo::WV_WINDOWS7) {
ui.centralWidget->setStyleSheet("QPushButton { padding: 2px; border: 1px solid #acacac; background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #eaeaea, stop:1 #e5e5e5);} QPushButton:hover { border: 1px solid #7eb4ea; background-color: #e4f0fc; }");
}
#endif
setMinimumSize(size());
setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
// Actions
connect(ui.screenPushButton, &QPushButton::clicked, this, &LightscreenWindow::screenHotkey);
connect(ui.areaPushButton , &QPushButton::clicked, this, &LightscreenWindow::areaHotkey);
connect(ui.windowPushButton, &QPushButton::clicked, this, &LightscreenWindow::windowPickerHotkey);
connect(ui.optionsPushButton, &QPushButton::clicked, this, &LightscreenWindow::showOptions);
connect(ui.folderPushButton , &QPushButton::clicked, this, &LightscreenWindow::goToFolder);
// Shortcuts
mGlobalHotkeys = new UGlobalHotkeys(this);
connect(mGlobalHotkeys, &UGlobalHotkeys::activated, [&](size_t id) {
action(id);
});
// Uploader
connect(Uploader::instance(), &Uploader::progressChanged, this, &LightscreenWindow::uploadProgress);
connect(Uploader::instance(), &Uploader::done , this, &LightscreenWindow::showUploaderMessage);
connect(Uploader::instance(), &Uploader::error , this, &LightscreenWindow::showUploaderError);
// Manager
connect(ScreenshotManager::instance(), &ScreenshotManager::confirm, this, &LightscreenWindow::preview);
connect(ScreenshotManager::instance(), &ScreenshotManager::windowCleanup, this, &LightscreenWindow::cleanup);
connect(ScreenshotManager::instance(), &ScreenshotManager::activeCountChange, this, &LightscreenWindow::updateStatus);
if (!settings()->contains("file/format")) {
showOptions(); // There are no options (or the options config is invalid or incomplete)
} else {
QTimer::singleShot(0 , this, &LightscreenWindow::applySettings);
QTimer::singleShot(5000, this, &LightscreenWindow::checkForUpdates);
}
}
LightscreenWindow::~LightscreenWindow()
{
settings()->setValue("lastScreenshot", mLastScreenshot);
settings()->sync();
mGlobalHotkeys->unregisterAllHotkeys();
}
void LightscreenWindow::action(int mode)
{
if (mode <= Screenshot::SelectedWindow) {
screenshotAction((Screenshot::Mode)mode);
} else if (mode == ShowMainWindow) {
show();
} else if (mode == OpenScreenshotFolder) {
goToFolder();
} else {
qWarning() << "Unknown hotkey ID: " << mode;
}
}
void LightscreenWindow::areaHotkey()
{
screenshotAction(Screenshot::SelectedArea);
}
void LightscreenWindow::checkForUpdates()
{
if (settings()->value("options/disableUpdater", false).toBool()) {
return;
}
if (settings()->value("lastUpdateCheck").toInt() + 7
> QDate::currentDate().dayOfYear()) {
return; // If 7 days have not passed since the last update check.
}
mUpdater = new Updater(this);
connect(mUpdater, &Updater::done, this, &LightscreenWindow::updaterDone);
mUpdater->check();
}
void LightscreenWindow::cleanup(const Screenshot::Options &options)
{
// Reversing settings
if (settings()->value("options/hide").toBool()) {
#ifndef Q_OS_LINUX // X is not quick enough and the notification ends up everywhere but in the icon
if (settings()->value("options/tray").toBool() && mTrayIcon) {
mTrayIcon->show();
}
#endif
if (mPreviewDialog) {
if (mPreviewDialog->count() <= 1 && mWasVisible) {
show();
}
mPreviewDialog->show();
} else if (mWasVisible) {
show();
}
mHideTrigger = false;
}
if (settings()->value("options/tray").toBool() && mTrayIcon) {
notify(options.result);
if (settings()->value("options/message").toBool() && options.file && !options.upload) {
// This message wll get shown only when messages are enabled and the file won't get another upload pop-up soon.
showScreenshotMessage(options.result, options.fileName);
}
}
if (settings()->value("options/playSound", false).toBool()) {
if (options.result == Screenshot::Success) {
QSound::play("sounds/ls.screenshot.wav");
} else {
#ifdef Q_OS_WIN
QSound::play("afakepathtomakewindowsplaythedefaultsoundtheresprobablyabetterwaybuticantbebothered");
#else
QSound::play("sound/ls.error.wav");
#endif
}
}
updateStatus();
if (options.result != Screenshot::Success) {
return;
}
mLastScreenshot = options.fileName;
}
void LightscreenWindow::closeToTrayWarning()
{
if (!settings()->value("options/closeToTrayWarning", true).toBool()) {
return;
}
mLastMessage = 3;
mTrayIcon->showMessage(tr("Closed to tray"), tr("Lightscreen will keep running, you can disable this in the options menu."));
settings()->setValue("options/closeToTrayWarning", false);
}
bool LightscreenWindow::closingWithoutTray()
{
if (settings()->value("options/disableHideAlert", false).toBool()) {
return false;
}
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Lightscreen"));
msgBox.setText(tr("You have chosen to hide Lightscreen when there's no system tray icon, so you will not be able to access the program <b>unless you have selected a hotkey to do so</b>.<br>What do you want to do?"));
msgBox.setIcon(QMessageBox::Warning);
msgBox.setStyleSheet("QPushButton { padding: 4px 8px; }");
auto enableButton = msgBox.addButton(tr("Hide but enable tray"), QMessageBox::ActionRole);
auto enableQuietButton = msgBox.addButton(tr("Hide and don't warn"), QMessageBox::ActionRole);
auto hideButton = msgBox.addButton(tr("Just hide"), QMessageBox::ActionRole);
auto abortButton = msgBox.addButton(QMessageBox::Cancel);
Q_UNUSED(abortButton);
msgBox.exec();
if (msgBox.clickedButton() == hideButton) {
return true;
} else if (msgBox.clickedButton() == enableQuietButton) {
settings()->setValue("options/disableHideAlert", true);
applySettings();
return true;
} else if (msgBox.clickedButton() == enableButton) {
settings()->setValue("options/tray", true);
applySettings();
return true;
}
return false; // Cancel.
}
void LightscreenWindow::createUploadMenu()
{
auto imgurMenu = new QMenu(tr("Upload"));
auto uploadAction = new QAction(os::icon("imgur"), tr("&Upload last"), imgurMenu);
uploadAction->setToolTip(tr("Upload the last screenshot you took to imgur.com"));
connect(uploadAction, &QAction::triggered, this, &LightscreenWindow::uploadLast);
auto cancelAction = new QAction(os::icon("no"), tr("&Cancel upload"), imgurMenu);
cancelAction->setToolTip(tr("Cancel the currently uploading screenshots"));
cancelAction->setEnabled(false);
connect(this, &LightscreenWindow::uploading, cancelAction, &QAction::setEnabled);
connect(cancelAction, &QAction::triggered, this, &LightscreenWindow::uploadCancel);
auto historyAction = new QAction(os::icon("view-history"), tr("View &History"), imgurMenu);
connect(historyAction, &QAction::triggered, this, &LightscreenWindow::showHistoryDialog);
imgurMenu->addAction(uploadAction);
imgurMenu->addAction(cancelAction);
imgurMenu->addAction(historyAction);
imgurMenu->addSeparator();
connect(imgurMenu, &QMenu::aboutToShow, this, [&, imgurMenu] {
imgurMenu->actions().at(0)->setEnabled(!mLastScreenshot.isEmpty());
});
ui.imgurPushButton->setMenu(imgurMenu);
}
void LightscreenWindow::goToFolder()
{
#ifdef Q_OS_WIN
if (!mLastScreenshot.isEmpty() && QFile::exists(mLastScreenshot)) {
QProcess::startDetached("explorer /select, \"" + mLastScreenshot + "\"");
} else {
#endif
QDir path(settings()->value("file/target").toString());
// We might want to go to the folder without it having been created by taking a screenshot yet.
if (!path.exists()) {
path.mkpath(path.absolutePath());
}
QDesktopServices::openUrl(QUrl::fromLocalFile(path.absolutePath() + QDir::separator()));
#ifdef Q_OS_WIN
}
#endif
}
void LightscreenWindow::messageClicked()
{
if (mLastMessage == 1) {
goToFolder();
} else if (mLastMessage == 3) {
QTimer::singleShot(0, this, &LightscreenWindow::showOptions);
} else {
QDesktopServices::openUrl(QUrl(Uploader::instance()->lastUrl()));
}
}
void LightscreenWindow::executeArgument(const QString &message)
{
if (message == "--wake") {
show();
os::setForegroundWindow(this);
qApp->alert(this, 2000);
} else if (message == "--screen") {
screenshotAction(Screenshot::WholeScreen);
} else if (message == "--area") {
screenshotAction(Screenshot::SelectedArea);
} else if (message == "--activewindow") {
screenshotAction(Screenshot::ActiveWindow);
} else if (message == "--pickwindow") {
screenshotAction(Screenshot::SelectedWindow);
} else if (message == "--folder") {
action(OpenScreenshotFolder);
} else if (message == "--uploadlast") {
uploadLast();
} else if (message == "--viewhistory") {
showHistoryDialog();
} else if (message == "--options") {
showOptions();
} else if (message == "--quit") {
qApp->quit();
}
}
void LightscreenWindow::executeArguments(const QStringList &arguments)
{
// If we just have the default argument, call "--wake"
if (arguments.count() == 1 && (arguments.at(0) == qApp->arguments().at(0) || arguments.at(0).contains(QFileInfo(qApp->applicationFilePath()).fileName()))) {
executeArgument("--wake");
return;
}
for (auto argument : arguments) {
executeArgument(argument);
}
}
void LightscreenWindow::notify(const Screenshot::Result &result)
{
switch (result) {
case Screenshot::Success:
mTrayIcon->setIcon(QIcon(":/icons/lightscreen.yes"));
if (mHasTaskbarButton) {
mTaskbarButton->setOverlayIcon(os::icon("yes"));
}
setWindowTitle(tr("Success!"));
break;
case Screenshot::Failure:
mTrayIcon->setIcon(QIcon(":/icons/lightscreen.no"));
setWindowTitle(tr("Failed!"));
if (mHasTaskbarButton) {
mTaskbarButton->setOverlayIcon(os::icon("no"));
}
break;
case Screenshot::Cancel:
setWindowTitle(tr("Cancelled!"));
break;
}
QTimer::singleShot(2000, this, &LightscreenWindow::restoreNotification);
}
void LightscreenWindow::preview(Screenshot *screenshot)
{
if (screenshot->options().preview) {
if (!mPreviewDialog) {
mPreviewDialog = new PreviewDialog(this);
}
mPreviewDialog->add(screenshot);
} else {
screenshot->confirm(true);
}
}
void LightscreenWindow::quit()
{
settings()->setValue("position", pos());
int answer = 0;
QString doing;
if (ScreenshotManager::instance()->activeCount() > 0) {
doing = tr("processing");
}
if (Uploader::instance()->uploading() > 0) {
if (doing.isEmpty()) {
doing = tr("uploading");
} else {
doing = tr("processing and uploading");
}
}
if (!doing.isEmpty()) {
answer = QMessageBox::question(this,
tr("Are you sure you want to quit?"),
tr("Lightscreen is currently %1 screenshots. Are you sure you want to quit?").arg(doing),
tr("Quit"),
tr("Don't Quit"));
}
if (answer == 0) {
emit finished();
}
}
void LightscreenWindow::restoreNotification()
{
if (mTrayIcon) {
mTrayIcon->setIcon(QIcon(":/icons/lightscreen.small"));
}
if (mHasTaskbarButton) {
mTaskbarButton->clearOverlayIcon();
mTaskbarButton->progress()->setVisible(false);
mTaskbarButton->progress()->stop();
mTaskbarButton->progress()->reset();
}
updateStatus();
}
void LightscreenWindow::screenshotAction(Screenshot::Mode mode)
{
int delayms = -1;
bool optionsHide = settings()->value("options/hide").toBool(); // Option cache, used a couple of times.
if (!mHideTrigger) {
mWasVisible = isVisible();
mHideTrigger = true;
}
// Applying pre-screenshot settings
if (optionsHide) {
hide();
#ifndef Q_OS_LINUX // X is not quick enough and the notification ends up everywhere but in the icon
if (mTrayIcon) {
mTrayIcon->hide();
}
#endif
}
// Screenshot delay
delayms = settings()->value("options/delay", 0).toInt();
delayms = delayms * 1000; // Converting the delay to milliseconds.
delayms += 400;
if (optionsHide && mPreviewDialog) {
if (mPreviewDialog->count() >= 1) {
mPreviewDialog->hide();
}
}
// The delayed functions works using the static variable lastMode
// which keeps the argument so a QTimer can call this function again.
if (delayms > 0) {
if (mLastMode == Screenshot::None) {
mLastMode = mode;
QTimer::singleShot(delayms, this, [&] {
screenshotAction(mLastMode);
});
return;
} else {
mode = mLastMode;
mLastMode = Screenshot::None;
}
}
static Screenshot::Options options;
if (!mDoCache) {
// Populating the option object that will then be passed to the screenshot engine (sounds fancy huh?)
options.file = settings()->value("file/enabled").toBool();
options.format = (Screenshot::Format) settings()->value("file/format").toInt();
options.prefix = settings()->value("file/prefix").toString();
QDir dir(settings()->value("file/target").toString());
dir.makeAbsolute();
options.directory = dir;
options.quality = settings()->value("options/quality", 100).toInt();
options.currentMonitor = settings()->value("options/currentMonitor", false).toBool();
options.clipboard = settings()->value("options/clipboard", true).toBool();
options.urlClipboard = settings()->value("options/urlClipboard", false).toBool();
options.preview = settings()->value("options/preview", false).toBool();
options.magnify = settings()->value("options/magnify", false).toBool();
options.cursor = settings()->value("options/cursor", true).toBool();
options.saveAs = settings()->value("options/saveAs", false).toBool();
options.animations = settings()->value("options/animations", true).toBool();
options.replace = settings()->value("options/replace", false).toBool();
options.upload = settings()->value("options/uploadAuto", false).toBool();
options.optimize = settings()->value("options/optipng", false).toBool();
options.uploadService = Uploader::serviceName(settings()->value("upload/service", 0).toInt());
Screenshot::NamingOptions namingOptions;
namingOptions.naming = (Screenshot::Naming) settings()->value("file/naming").toInt();
namingOptions.leadingZeros = settings()->value("options/naming/leadingZeros", 0).toInt();
namingOptions.flip = settings()->value("options/flip", false).toBool();
namingOptions.dateFormat = settings()->value("options/naming/dateFormat", "yyyy-MM-dd").toString();
options.namingOptions = namingOptions;
mDoCache = true;
}
options.mode = mode;
ScreenshotManager::instance()->take(options);
}
void LightscreenWindow::screenshotActionTriggered(QAction *action)
{
screenshotAction((Screenshot::Mode) action->data().toInt());
}
void LightscreenWindow::screenHotkey()
{
screenshotAction(Screenshot::WholeScreen);
}
void LightscreenWindow::showHotkeyError(const QStringList &hotkeys)
{
static bool dontShow = false;
if (dontShow) {
return;
}
QString messageText;
messageText = tr("Some hotkeys could not be registered, they might already be in use");
if (hotkeys.count() > 1) {
messageText += tr("<br>The failed hotkeys are the following:") + "<ul>";
for (auto hotkey : hotkeys) {
messageText += QString("%1%2%3").arg("<li><b>").arg(hotkey).arg("</b></li>");
}
messageText += "</ul>";
} else {
messageText += tr("<br>The failed hotkey is <b>%1</b>").arg(hotkeys[0]);
}
messageText += tr("<br><i>What do you want to do?</i>");
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Lightscreen"));
msgBox.setText(messageText);
QPushButton *changeButton = msgBox.addButton(tr("Change") , QMessageBox::ActionRole);
QPushButton *disableButton = msgBox.addButton(tr("Disable"), QMessageBox::ActionRole);
QPushButton *exitButton = msgBox.addButton(tr("Quit") , QMessageBox::ActionRole);
msgBox.exec();
if (msgBox.clickedButton() == exitButton) {
dontShow = true;
- QTimer::singleShot(10, this, SLOT(quit()));
+ QTimer::singleShot(10, this, &LightscreenWindow::quit);
} else if (msgBox.clickedButton() == changeButton) {
showOptions();
} else if (msgBox.clickedButton() == disableButton) {
for (auto hotkey : hotkeys) {
settings()->setValue(QString("actions/%1/enabled").arg(hotkey), false);
}
}
}
void LightscreenWindow::showHistoryDialog()
{
HistoryDialog historyDialog(this);
historyDialog.exec();
updateStatus();
}
void LightscreenWindow::showOptions()
{
mGlobalHotkeys->unregisterAllHotkeys();
QPointer<OptionsDialog> optionsDialog = new OptionsDialog(this);
optionsDialog->exec();
optionsDialog->deleteLater();
applySettings();
}
void LightscreenWindow::showScreenshotMessage(const Screenshot::Result &result, const QString &fileName)
{
if (result == Screenshot::Cancel) {
return;
}
// Showing message.
QString title;
QString message;
if (result == Screenshot::Success) {
title = QFileInfo(fileName).fileName();
if (settings()->value("file/target").toString().isEmpty()) {
message = QDir::toNativeSeparators(QCoreApplication::applicationDirPath());
} else {
message = tr("Saved to \"%1\"").arg(settings()->value("file/target").toString());
}
} else {
title = tr("The screenshot was not taken");
message = tr("An error occurred.");
}
mLastMessage = 1;
mTrayIcon->showMessage(title, message);
}
void LightscreenWindow::showUploaderError(const QString &error)
{
mLastMessage = -1;
updateStatus();
if (mTrayIcon && !error.isEmpty() && settings()->value("options/message").toBool()) {
mTrayIcon->showMessage(tr("Upload error"), error);
}
notify(Screenshot::Failure);
}
void LightscreenWindow::showUploaderMessage(const QString &fileName, const QString &url)
{
if (mTrayIcon && settings()->value("options/message").toBool() && !url.isEmpty()) {
QString screenshot = QFileInfo(fileName).fileName();
if (screenshot.startsWith(".lstemp.")) {
screenshot = tr("Screenshot");
}
mLastMessage = 2;
mTrayIcon->showMessage(tr("%1 uploaded").arg(screenshot), tr("Click here to go to %1").arg(url));
}
updateStatus();
}
void LightscreenWindow::toggleVisibility()
{
if (isVisible()) {
hide();
} else {
show();
os::setForegroundWindow(this);
}
}
void LightscreenWindow::updateStatus()
{
int uploadCount = Uploader::instance()->uploading();
int activeCount = ScreenshotManager::instance()->activeCount();
if (mHasTaskbarButton) {
mTaskbarButton->progress()->setPaused(true);
mTaskbarButton->progress()->setVisible(true);
}
if (uploadCount > 0) {
setStatus(tr("%1 uploading").arg(uploadCount));
if (mHasTaskbarButton) {
mTaskbarButton->progress()->setRange(0, 100);
mTaskbarButton->progress()->resume();
}
emit uploading(true);
} else {
if (activeCount > 1) {
setStatus(tr("%1 processing").arg(activeCount));
} else if (activeCount == 1) {
setStatus(tr("processing"));
} else {
setStatus();
if (mHasTaskbarButton) {
mTaskbarButton->progress()->setVisible(false);
}
}
emit uploading(false);
}
}
void LightscreenWindow::updaterDone(bool result)
{
mUpdater->deleteLater();
settings()->setValue("lastUpdateCheck", QDate::currentDate().dayOfYear());
if (!result) {
return;
}
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Lightscreen"));
msgBox.setText(tr("There's a new version of Lightscreen available.<br>Would you like to see more information?<br>(<em>You can turn this notification off</em>)"));
msgBox.setIcon(QMessageBox::Information);
QPushButton *yesButton = msgBox.addButton(QMessageBox::Yes);
QPushButton *turnOffButton = msgBox.addButton(tr("Turn Off"), QMessageBox::ActionRole);
QPushButton *remindButton = msgBox.addButton(tr("Remind Me Later"), QMessageBox::RejectRole);
Q_UNUSED(remindButton);
msgBox.exec();
if (msgBox.clickedButton() == yesButton) {
QDesktopServices::openUrl(QUrl("https://lightscreen.com.ar/whatsnew?from=" + qApp->applicationVersion()));
} else if (msgBox.clickedButton() == turnOffButton) {
settings()->setValue("options/disableUpdater", true);
}
}
void LightscreenWindow::upload(const QString &fileName)
{
Uploader::instance()->upload(fileName, Uploader::serviceName(settings()->value("upload/service", 0).toInt()));
}
void LightscreenWindow::uploadCancel()
{
if (Uploader::instance()->uploading() <= 0) {
return;
}
int confirm = QMessageBox::question(this, tr("Upload cancel"), tr("Do you want to cancel all screenshot uploads?"), tr("Cancel"), tr("Don't Cancel"));
if (confirm == 0) {
Uploader::instance()->cancel();
updateStatus();
}
}
void LightscreenWindow::uploadLast()
{
upload(mLastScreenshot);
updateStatus();
}
void LightscreenWindow::uploadProgress(int progress)
{
if (mHasTaskbarButton) {
mTaskbarButton->progress()->setVisible(true);
mTaskbarButton->progress()->setValue(progress);
}
if (isVisible() && progress > 0) {
int uploadCount = Uploader::instance()->uploading();
if (uploadCount > 1) {
setWindowTitle(tr("%1% of %2 uploads - Lightscreen").arg(progress).arg(uploadCount));
} else {
setWindowTitle(tr("%1% - Lightscreen").arg(progress));
}
}
}
void LightscreenWindow::windowHotkey()
{
screenshotAction(Screenshot::ActiveWindow);
}
void LightscreenWindow::windowPickerHotkey()
{
screenshotAction(Screenshot::SelectedWindow);
}
void LightscreenWindow::applySettings()
{
bool tray = settings()->value("options/tray", true).toBool();
if (tray && !mTrayIcon) {
createTrayIcon();
mTrayIcon->setVisible(true);
} else if (!tray && mTrayIcon) {
mTrayIcon->setVisible(false);
}
connectHotkeys();
mDoCache = false;
if (settings()->value("lastScreenshot").isValid() && mLastScreenshot.isEmpty()) {
mLastScreenshot = settings()->value("lastScreenshot").toString();
}
os::setStartup(settings()->value("options/startup").toBool(), settings()->value("options/startupHide").toBool());
}
void LightscreenWindow::connectHotkeys()
{
const QStringList actions = {"screen", "window", "area", "windowPicker", "open", "directory"};
QStringList failed;
size_t id = Screenshot::WholeScreen;
for (auto action : actions) {
if (settings()->value("actions/" + action + "/enabled").toBool()) {
if (!mGlobalHotkeys->registerHotkey(settings()->value("actions/" + action + "/hotkey").toString(), id)) {
failed << action;
}
}
id++;
}
if (!failed.isEmpty()) {
showHotkeyError(failed);
}
}
void LightscreenWindow::createTrayIcon()
{
mTrayIcon = new QSystemTrayIcon(QIcon(":/icons/lightscreen.small"), this);
updateStatus();
connect(mTrayIcon, &QSystemTrayIcon::messageClicked, this, &LightscreenWindow::messageClicked);
connect(mTrayIcon, &QSystemTrayIcon::activated , this, [&](QSystemTrayIcon::ActivationReason reason) {
if (reason != QSystemTrayIcon::DoubleClick) return;
toggleVisibility();
});
auto hideAction = new QAction(QIcon(":/icons/lightscreen.small"), tr("Show&/Hide"), mTrayIcon);
connect(hideAction, &QAction::triggered, this, &LightscreenWindow::toggleVisibility);
auto screenAction = new QAction(os::icon("screen"), tr("&Screen"), mTrayIcon);
screenAction->setData(QVariant(0));
auto windowAction = new QAction(os::icon("window"), tr("Active &Window"), this);
windowAction->setData(QVariant(1));
auto windowPickerAction = new QAction(os::icon("pickWindow"), tr("&Pick Window"), this);
windowPickerAction->setData(QVariant(3));
auto areaAction = new QAction(os::icon("area"), tr("&Area"), mTrayIcon);
areaAction->setData(QVariant(2));
auto screenshotGroup = new QActionGroup(mTrayIcon);
screenshotGroup->addAction(screenAction);
screenshotGroup->addAction(areaAction);
screenshotGroup->addAction(windowAction);
screenshotGroup->addAction(windowPickerAction);
connect(screenshotGroup, &QActionGroup::triggered, this, &LightscreenWindow::screenshotActionTriggered);
// Duplicated for the screenshot button :(
auto uploadAction = new QAction(os::icon("imgur"), tr("&Upload last"), mTrayIcon);
uploadAction->setToolTip(tr("Upload the last screenshot you took to imgur.com"));
connect(uploadAction, &QAction::triggered, this, &LightscreenWindow::uploadLast);
auto cancelAction = new QAction(os::icon("no"), tr("&Cancel upload"), mTrayIcon);
cancelAction->setToolTip(tr("Cancel the currently uploading screenshots"));
cancelAction->setEnabled(false);
connect(this, &LightscreenWindow::uploading, cancelAction, &QAction::setEnabled);
connect(cancelAction, &QAction::triggered, this, &LightscreenWindow::uploadCancel);
auto historyAction = new QAction(os::icon("view-history"), tr("View History"), mTrayIcon);
connect(historyAction, &QAction::triggered, this, &LightscreenWindow::showHistoryDialog);
//
auto optionsAction = new QAction(os::icon("configure"), tr("View &Options"), mTrayIcon);
connect(optionsAction, &QAction::triggered, this, &LightscreenWindow::showOptions);
auto goAction = new QAction(os::icon("folder"), tr("&Go to Folder"), mTrayIcon);
connect(goAction, &QAction::triggered, this, &LightscreenWindow::goToFolder);
auto quitAction = new QAction(tr("&Quit"), mTrayIcon);
connect(quitAction, &QAction::triggered, this, &LightscreenWindow::quit);
auto screenshotMenu = new QMenu(tr("Screenshot"));
screenshotMenu->addAction(screenAction);
screenshotMenu->addAction(areaAction);
screenshotMenu->addAction(windowAction);
screenshotMenu->addAction(windowPickerAction);
// Duplicated for the screenshot button :(
auto imgurMenu = new QMenu(tr("Upload"));
imgurMenu->addAction(uploadAction);
imgurMenu->addAction(cancelAction);
imgurMenu->addAction(historyAction);
imgurMenu->addSeparator();
auto trayIconMenu = new QMenu;
trayIconMenu->addAction(hideAction);
trayIconMenu->addSeparator();
trayIconMenu->addMenu(imgurMenu);
trayIconMenu->addSeparator();
trayIconMenu->addMenu(screenshotMenu);
trayIconMenu->addAction(optionsAction);
trayIconMenu->addAction(goAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
mTrayIcon->setContextMenu(trayIconMenu);
}
void LightscreenWindow::setStatus(QString status)
{
if (status.isEmpty()) {
status = tr("Lightscreen");
} else {
status += tr(" - Lightscreen");
}
if (mTrayIcon) {
mTrayIcon->setToolTip(status);
}
setWindowTitle(status);
}
QSettings *LightscreenWindow::settings() const
{
return ScreenshotManager::instance()->settings();
}
// Event handling
bool LightscreenWindow::event(QEvent *event)
{
if (event->type() == QEvent::Show) {
QPoint savedPosition = settings()->value("position").toPoint();
if (!savedPosition.isNull() && qApp->desktop()->availableGeometry().contains(QRect(savedPosition, size()))) {
move(savedPosition);
}
if (mHasTaskbarButton) {
mTaskbarButton->setWindow(windowHandle());
}
} else if (event->type() == QEvent::Hide) {
settings()->setValue("position", pos());
} else if (event->type() == QEvent::Close) {
if (settings()->value("options/tray").toBool() && settings()->value("options/closeHide").toBool()) {
closeToTrayWarning();
hide();
} else if (settings()->value("options/closeHide").toBool()) {
if (closingWithoutTray()) {
hide();
}
} else {
quit();
}
} else if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
#ifdef Q_WS_MAC
if (keyEvent->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) {
keyEvent->ignore();
if (isVisible()) {
toggleVisibility();
}
return false;
} else
#endif
if (!keyEvent->modifiers() && keyEvent->key() == Qt::Key_Escape) {
keyEvent->ignore();
if (isVisible()) {
toggleVisibility();
}
return false;
}
} else if (event->type() == QEvent::LanguageChange) {
ui.retranslateUi(this);
resize(minimumSizeHint());
}
return QMainWindow::event(event);
}
diff --git a/tools/screenshotmanager.cpp b/tools/screenshotmanager.cpp
index d629d46..bbeaeea 100644
--- a/tools/screenshotmanager.cpp
+++ b/tools/screenshotmanager.cpp
@@ -1,233 +1,233 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "screenshotmanager.h"
#include "screenshot.h"
#include "uploader/uploader.h"
#include <QApplication>
#include <QDateTime>
#include <QStandardPaths>
#include <QFile>
#include <QSettings>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
ScreenshotManager::ScreenshotManager(QObject *parent) : QObject(parent), mHistoryInitialized(false)
{
if (QFile::exists(qApp->applicationDirPath() + QDir::separator() + "config.ini")) {
mSettings = new QSettings(qApp->applicationDirPath() + QDir::separator() + "config.ini", QSettings::IniFormat, this);
mPortableMode = true;
mHistoryPath = qApp->applicationDirPath() + QDir::separator();
} else {
mSettings = new QSettings(this);
mPortableMode = false;
mHistoryPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator();
}
connect(Uploader::instance(), &Uploader::done, this, &ScreenshotManager::uploadDone);
}
void ScreenshotManager::initHistory()
{
if (mHistoryInitialized) {
return;
}
// Creating the SQLite database.
QSqlDatabase history = QSqlDatabase::addDatabase("QSQLITE");
QDir historyPath(mHistoryPath);
if (!historyPath.exists()) {
historyPath.mkpath(mHistoryPath);
}
history.setHostName("localhost");
history.setDatabaseName(mHistoryPath + "history.sqlite");
if (history.open()) {
QSqlQuery tableQuery;
mHistoryInitialized = tableQuery.exec("CREATE TABLE IF NOT EXISTS history (fileName text, URL text, deleteURL text, time integer)");
history.exec("CREATE INDEX IF NOT EXISTS fileName_index ON history(fileName)");
} else {
qCritical() << "Could not open SQLite DB.";
mHistoryInitialized = false;
}
}
int ScreenshotManager::activeCount() const
{
return mScreenshots.count();
}
bool ScreenshotManager::portableMode()
{
return mPortableMode;
}
void ScreenshotManager::saveHistory(const QString &fileName, const QString &url, const QString &deleteHash)
{
if (!mSettings->value("/options/history", true).toBool()) {
return;
}
if (!mHistoryInitialized) {
initHistory();
}
QString deleteUrl;
if (!deleteHash.isEmpty()) {
deleteUrl = "https://imgur.com/delete/" + deleteHash;
}
QSqlQuery saveHistoryQuery;
saveHistoryQuery.prepare("INSERT INTO history (fileName, URL, deleteURL, time) VALUES(?, ?, ?, ?)");
saveHistoryQuery.addBindValue(fileName);
saveHistoryQuery.addBindValue(url);
saveHistoryQuery.addBindValue(deleteUrl);
saveHistoryQuery.addBindValue(QDateTime::currentMSecsSinceEpoch());
saveHistoryQuery.exec();
}
void ScreenshotManager::updateHistory(const QString &fileName, const QString &url, const QString &deleteHash)
{
if (!mSettings->value("/options/history", true).toBool() || url.isEmpty()) {
return;
}
if (!mHistoryInitialized) {
initHistory();
}
QSqlQuery query;
query.prepare("SELECT fileName FROM history WHERE URL IS NOT EMPTY AND fileName = ?");
query.addBindValue(fileName);
query.exec();
if (query.record().count() > 0) {
QSqlQuery updateQuery;
updateQuery.prepare("UPDATE history SET URL = ?, deleteURL = ?, time = ? WHERE fileName = ?");
updateQuery.addBindValue(url);
updateQuery.addBindValue("https://imgur.com/delete/" + deleteHash);
updateQuery.addBindValue(QDateTime::currentMSecsSinceEpoch());
updateQuery.addBindValue(fileName);
updateQuery.exec();
} else {
saveHistory(fileName, url, deleteHash);
}
}
void ScreenshotManager::removeHistory(const QString &fileName, qint64 time)
{
if (!mHistoryInitialized) {
initHistory();
}
QSqlQuery removeQuery;
removeQuery.prepare("DELETE FROM history WHERE fileName = ? AND time = ?");
removeQuery.addBindValue(fileName);
removeQuery.addBindValue(time);
removeQuery.exec();
}
void ScreenshotManager::clearHistory()
{
if (!mHistoryInitialized) {
initHistory();
}
QSqlQuery deleteQuery("DELETE FROM history");
deleteQuery.exec();
QSqlQuery vacQuery("VACUUM");
vacQuery.exec();
}
//
void ScreenshotManager::askConfirmation()
{
Screenshot *s = qobject_cast<Screenshot *>(sender());
emit confirm(s);
}
void ScreenshotManager::cleanup()
{
Screenshot *screenshot = qobject_cast<Screenshot *>(sender());
emit windowCleanup(screenshot->options());
}
void ScreenshotManager::finished()
{
Screenshot *screenshot = qobject_cast<Screenshot *>(sender());
mScreenshots.removeOne(screenshot);
emit activeCountChange();
screenshot->deleteLater();
}
void ScreenshotManager::take(Screenshot::Options &options)
{
Screenshot *newScreenshot = new Screenshot(this, options);
mScreenshots.append(newScreenshot);
- connect(newScreenshot, SIGNAL(askConfirmation()), this, SLOT(askConfirmation()));
- connect(newScreenshot, SIGNAL(cleanup()) , this, SLOT(cleanup()));
- connect(newScreenshot, SIGNAL(finished()) , this, SLOT(finished()));
+ connect(newScreenshot, &Screenshot::askConfirmation, this, &ScreenshotManager::askConfirmation);
+ connect(newScreenshot, &Screenshot::cleanup , this, &ScreenshotManager::cleanup);
+ connect(newScreenshot, &Screenshot::finished , this, &ScreenshotManager::finished);
newScreenshot->take();
}
void ScreenshotManager::uploadDone(const QString &fileName, const QString &url, const QString &deleteHash)
{
for (Screenshot *screenshot : qAsConst(mScreenshots)) {
if (screenshot->options().fileName == fileName
|| screenshot->unloadedFileName() == fileName) {
screenshot->uploadDone(url);
if (screenshot->options().file) {
updateHistory(fileName, url, deleteHash);
} else {
saveHistory("", url, deleteHash);
}
return;
}
}
// If we get here, it's because the screenshot upload wasn't on the current screenshot list, which means it's a View History/Upload Later upload.
updateHistory(fileName, url, deleteHash);
}
// Singleton
ScreenshotManager *ScreenshotManager::mInstance = nullptr;
ScreenshotManager *ScreenshotManager::instance()
{
if (!mInstance) {
mInstance = new ScreenshotManager();
}
return mInstance;
}
diff --git a/tools/uploader/imguruploader.cpp b/tools/uploader/imguruploader.cpp
index f621388..cc93205 100644
--- a/tools/uploader/imguruploader.cpp
+++ b/tools/uploader/imguruploader.cpp
@@ -1,209 +1,209 @@
#include "imguruploader.h"
#include "../uploader/uploader.h"
#include <QNetworkAccessManager>
#include <QtNetwork>
#include <QJsonDocument>
#include <QJsonObject>
ImgurUploader::ImgurUploader(QObject *parent) : ImageUploader(parent)
{
mUploaderType = "imgur";
loadSettings();
}
const QString ImgurUploader::clientId()
{
return QString("3ebe94c791445c1");
}
const QString ImgurUploader::clientSecret()
{
return QString("0546b05d6a80b2092dcea86c57b792c9c9faebf0");
}
void ImgurUploader::authorize(const QString &pin, AuthorizationCallback callback)
{
if (pin.isEmpty()) {
callback(false);
return;
}
QByteArray parameters;
parameters.append(QString("client_id=").toUtf8());
parameters.append(QUrl::toPercentEncoding(clientId()));
parameters.append(QString("&client_secret=").toUtf8());
parameters.append(QUrl::toPercentEncoding(clientSecret()));
parameters.append(QString("&grant_type=pin").toUtf8());
parameters.append(QString("&pin=").toUtf8());
parameters.append(QUrl::toPercentEncoding(pin));
QNetworkRequest request(QUrl("https://api.imgur.com/oauth2/token"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = Uploader::network()->post(request, parameters);
authorizationReply(reply, callback);
}
void ImgurUploader::refreshAuthorization(const QString &refresh_token, AuthorizationCallback callback)
{
if (refresh_token.isEmpty()) {
callback(false);
return;
}
QByteArray parameters;
parameters.append(QString("refresh_token=").toUtf8());
parameters.append(QUrl::toPercentEncoding(refresh_token));
parameters.append(QString("&client_id=").toUtf8());
parameters.append(QUrl::toPercentEncoding(clientId()));
parameters.append(QString("&client_secret=").toUtf8());
parameters.append(QUrl::toPercentEncoding(clientSecret()));
parameters.append(QString("&grant_type=refresh_token").toUtf8());
QNetworkRequest request(QUrl("https://api.imgur.com/oauth2/token"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = Uploader::network()->post(request, parameters);
authorizationReply(reply, callback);
}
void ImgurUploader::upload(const QString &fileName)
{
QFile *file = new QFile(fileName);
if (!file->open(QIODevice::ReadOnly)) {
emit error(ImageUploader::FileError, tr("Unable to read screenshot file"), fileName);
file->deleteLater();
return;
}
QNetworkRequest request(QUrl("https://api.imgur.com/3/image"));
request.setRawHeader("Authorization", QString("Client-ID %1").arg(clientId()).toLatin1());
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
if (!mSettings.value("anonymous", true).toBool()) {
request.setRawHeader("Authorization", QByteArray("Bearer ") + mSettings.value("access_token").toByteArray());
if (!mSettings.value("album").toString().isEmpty()) {
QHttpPart albumPart;
albumPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"album\""));
albumPart.setBody(mSettings.value("album").toByteArray());
multiPart->append(albumPart);
}
}
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QMimeDatabase().mimeTypeForFile(fileName, QMimeDatabase::MatchExtension).name());
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
imagePart.setBodyDevice(file);
file->setParent(multiPart);
multiPart->append(imagePart);
QNetworkReply *reply = Uploader::network()->post(request, multiPart);
reply->setProperty("fileName", fileName);
this->setProperty("fileName", fileName);
multiPart->setParent(reply);
- connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(uploadProgress(qint64, qint64)));
- connect(this , SIGNAL(cancelRequest()), reply, SLOT(abort()));
- connect(this , SIGNAL(cancelRequest()), reply, SLOT(deleteLater()));
+ connect(reply, &QNetworkReply::uploadProgress, this, &ImgurUploader::uploadProgress);
+ connect(this , &ImgurUploader::cancelRequest, reply, &QNetworkReply::abort);
+ connect(this , &ImgurUploader::cancelRequest, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::sslErrors, [reply](const QList<QSslError> &errors) {
Q_UNUSED(errors);
if (QSysInfo::WindowsVersion == QSysInfo::WV_XP) {
reply->ignoreSslErrors();
}
});
- connect(reply, SIGNAL(finished()), this, SLOT(finished()));
+ connect(reply, &QNetworkReply::finished, this, &ImgurUploader::finished);
}
void ImgurUploader::retry()
{
loadSettings();
upload(property("fileName").toString());
}
void ImgurUploader::cancel()
{
emit cancelRequest();
}
void ImgurUploader::finished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
reply->deleteLater();
QString fileName = reply->property("fileName").toString();
if (reply->error() != QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::OperationCanceledError) {
emit error(ImageUploader::CancelError, "", fileName);
} else if (reply->error() == QNetworkReply::ContentOperationNotPermittedError ||
reply->error() == QNetworkReply::AuthenticationRequiredError) {
refreshAuthorization(mSettings["refresh_token"].toString(), [&](bool result) {
if (result) {
QTimer::singleShot(50, this, &ImgurUploader::retry);
} else {
cancel();
emit error(ImageUploader::AuthorizationError, tr("Imgur user authentication failed"), fileName);
}
});
} else {
emit error(ImageUploader::NetworkError, reply->errorString(), fileName);
}
return;
}
if (reply->rawHeader("X-RateLimit-Remaining") == "0") {
emit error(ImageUploader::HostError, tr("Imgur upload limit reached"), fileName);
return;
}
QJsonObject imgurResponse = QJsonDocument::fromJson(reply->readAll()).object();
if (imgurResponse.value("success").toBool() == true && imgurResponse.value("status").toInt() == 200) {
QJsonObject imageData = imgurResponse.value("data").toObject();
emit uploaded(fileName, imageData["link"].toString(), imageData["deletehash"].toString());
} else {
emit error(ImageUploader::HostError, tr("Imgur error"), fileName);
}
}
void ImgurUploader::uploadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
float b = (float) bytesReceived / bytesTotal;
int p = qRound(b * 100);
setProgress(p);
}
void ImgurUploader::authorizationReply(QNetworkReply *reply, AuthorizationCallback callback)
{
connect(reply, &QNetworkReply::finished, [reply, callback] {
reply->deleteLater();
bool authorized = false;
const QJsonObject imgurResponse = QJsonDocument::fromJson(reply->readAll()).object();
if (!imgurResponse.isEmpty() && imgurResponse.contains("access_token")) {
QVariantHash newSettings;
newSettings["access_token"] = imgurResponse.value("access_token").toString();
newSettings["refresh_token"] = imgurResponse.value("refresh_token").toString();
newSettings["account_username"] = imgurResponse.value("account_username").toString();
newSettings["expires_in"] = imgurResponse.value("expires_in").toInt();
ImgurUploader::saveSettings("imgur", newSettings);
authorized = true;
}
callback(authorized);
});
}
diff --git a/tools/uploader/uploader.cpp b/tools/uploader/uploader.cpp
index 0cf9bdd..fc33c05 100644
--- a/tools/uploader/uploader.cpp
+++ b/tools/uploader/uploader.cpp
@@ -1,141 +1,141 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "uploader.h"
#include "../screenshotmanager.h"
#include "imguruploader.h"
#include <QtNetwork>
#include <QSettings>
Uploader *Uploader::mInstance = 0;
Uploader::Uploader(QObject *parent) : QObject(parent), mProgress(0)
{
mNetworkAccessManager = new QNetworkAccessManager(this);
}
Uploader *Uploader::instance()
{
if (!mInstance) {
mInstance = new Uploader();
}
return mInstance;
}
QNetworkAccessManager *Uploader::network()
{
return instance()->mNetworkAccessManager;
}
int Uploader::progress() const
{
return mProgress;
}
QString Uploader::serviceName(int index)
{ // TODO: Move somewhere else? Use indexes everywhere? an enum?
switch (index) {
case 1:
return "pomf";
case 0:
default:
return "imgur";
}
}
QString Uploader::lastUrl() const
{
return mLastUrl;
}
void Uploader::cancel()
{
mUploaders.clear();
mProgress = 0;
emit cancelAll();
}
void Uploader::upload(const QString &fileName, const QString &uploadService)
{
if (fileName.isEmpty()) {
return;
}
auto uploader = ImageUploader::factory(uploadService);
connect(uploader, &ImageUploader::progressChanged, this , &Uploader::reportProgress);
connect(this , &Uploader::cancelAll , uploader, &ImageUploader::cancel);
connect(uploader, &ImageUploader::error, [&, uploader](ImageUploader::Error errorCode, const QString &errorString, const QString &fileName) {
mUploaders.removeAll(uploader);
uploader->deleteLater();
mProgress = 0; // TODO: ?
if (errorCode != ImageUploader::CancelError) {
if (errorString.isEmpty()) {
emit error(tr("Upload Error %1").arg(errorCode));
} else {
emit error(errorString);
}
}
emit done(fileName, "", "");
});
connect(uploader, &ImageUploader::uploaded, [&, uploader](const QString &file, const QString &url, const QString &deleteHash) {
mLastUrl = url;
mUploaders.removeAll(uploader);
if (mUploaders.isEmpty()) {
mProgress = 0;
}
uploader->deleteLater();
emit done(file, url, deleteHash);
});
mUploaders.append(uploader);
uploader->upload(fileName);
}
-int Uploader::uploading()
+int Uploader::uploading() const
{
return mUploaders.count();
}
void Uploader::reportProgress(int progress)
{
if (mUploaders.size() <= 0) {
mProgress = progress;
emit progressChanged(progress);
return;
}
int totalProgress = 0;
for (int i = 0; i < mUploaders.size(); ++i) {
totalProgress += mUploaders[i]->progress();
}
mProgress = totalProgress / mUploaders.size();
emit progressChanged(mProgress);
}
diff --git a/tools/uploader/uploader.h b/tools/uploader/uploader.h
index a9806e3..009c90a 100644
--- a/tools/uploader/uploader.h
+++ b/tools/uploader/uploader.h
@@ -1,63 +1,63 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef UPLOADER_H
#define UPLOADER_H
#include <QObject>
#include <QtNetwork>
#include "imageuploader.h"
class Uploader : public QObject
{
Q_OBJECT
Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
Q_PROPERTY(QString lastUrl READ lastUrl)
public:
Uploader(QObject *parent = 0);
static Uploader *instance();
static QNetworkAccessManager *network();
static QString serviceName(int index);
QString lastUrl() const;
int progress() const;
+ int uploading() const;
public slots:
void cancel();
void upload(const QString &fileName, const QString &uploadService);
- int uploading();
void reportProgress(int progressChanged);
signals:
void done(const QString &fileName, const QString &url, const QString &deleteHash);
void error(const QString &errorString);
void progressChanged(int progressChanged);
void cancelAll();
private:
static Uploader *mInstance;
QNetworkAccessManager *mNetworkAccessManager;
int mProgress;
QString mLastUrl;
QList<ImageUploader *> mUploaders;
};
#endif // UPLOADER_H
diff --git a/tools/windowpicker.cpp b/tools/windowpicker.cpp
index ce3af20..87caa2c 100644
--- a/tools/windowpicker.cpp
+++ b/tools/windowpicker.cpp
@@ -1,304 +1,304 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QApplication>
#include <QDesktopWidget>
#include <QLabel>
#include <QMouseEvent>
#include <QPushButton>
#include <QRubberBand>
#include <QRubberBand>
#include <QVBoxLayout>
#include <QWidget>
#include "windowpicker.h"
#include "os.h"
#if defined(Q_OS_WIN)
#include <QtWin>
#include <windows.h>
#ifdef _WIN64
#define GCL_HICON GCLP_HICON
#define GCL_HICONSM GCLP_HICONSM
#endif
#elif defined(Q_OS_LINUX)
#include <QX11Info>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#endif
WindowPicker::WindowPicker() : QWidget(0), mCrosshair(":/icons/picker"), mWindowLabel(Q_NULLPTR), mTaken(false), mCurrentWindow(0)
{
#if defined(Q_OS_WIN)
setWindowFlags(Qt::SplashScreen | Qt::WindowStaysOnTopHint);
#elif defined(Q_OS_LINUX)
setWindowFlags(Qt::WindowStaysOnTopHint);
#endif
setWindowTitle(tr("Lightscreen Window Picker"));
setStyleSheet("QWidget { color: #000; } #frame { padding: 7px 10px; border: 4px solid #232323; background-color: rgba(250, 250, 250, 255); }");
QLabel *helpLabel = new QLabel(tr("Grab the window picker by clicking and holding down the mouse button, then drag it to the window of your choice and release it to capture."), this);
helpLabel->setMinimumWidth(400);
helpLabel->setMaximumWidth(400);
helpLabel->setWordWrap(true);
mWindowIcon = new QLabel(this);
mWindowIcon->setMinimumSize(22, 22);
mWindowIcon->setMaximumSize(22, 22);
mWindowIcon->setScaledContents(true);
mWindowLabel = new QLabel(tr(" - Start dragging to select windows"), this);
mWindowLabel->setStyleSheet("font-weight: bold");
mCrosshairLabel = new QLabel(this);
mCrosshairLabel->setAlignment(Qt::AlignHCenter);
mCrosshairLabel->setPixmap(mCrosshair);
QPushButton *closeButton = new QPushButton(tr("Close"));
- connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
+ connect(closeButton, &QPushButton::clicked, this, &WindowPicker::close);
QHBoxLayout *windowLayout = new QHBoxLayout;
windowLayout->addWidget(mWindowIcon);
windowLayout->addWidget(mWindowLabel);
windowLayout->setMargin(0);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(0);
buttonLayout->addWidget(closeButton);
buttonLayout->setMargin(0);
QHBoxLayout *crosshairLayout = new QHBoxLayout;
crosshairLayout->addStretch(0);
crosshairLayout->addWidget(mCrosshairLabel);
crosshairLayout->addStretch(0);
crosshairLayout->setMargin(0);
QVBoxLayout *fl = new QVBoxLayout;
fl->addWidget(helpLabel);
fl->addLayout(windowLayout);
fl->addLayout(crosshairLayout);
fl->addLayout(buttonLayout);
fl->setMargin(0);
QFrame *frame = new QFrame(this);
frame->setObjectName("frame");
frame->setLayout(fl);
QVBoxLayout *l = new QVBoxLayout;
l->setMargin(0);
l->addWidget(frame);
setLayout(l);
resize(sizeHint());
move(QApplication::desktop()->screenGeometry(QApplication::desktop()->screenNumber(QCursor::pos())).center() - QPoint(width() / 2, height() / 2));
show();
}
WindowPicker::~WindowPicker()
{
qApp->restoreOverrideCursor();
}
void WindowPicker::cancel()
{
mWindowIcon->setPixmap(QPixmap());
mCrosshairLabel->setPixmap(mCrosshair);
qApp->restoreOverrideCursor();
}
void WindowPicker::closeEvent(QCloseEvent *)
{
if (!mTaken) {
emit pixmap(QPixmap());
}
qApp->restoreOverrideCursor();
deleteLater();
}
void WindowPicker::mouseMoveEvent(QMouseEvent *event)
{
QString windowName;
#if defined(Q_OS_WIN)
POINT mousePos;
mousePos.x = event->globalX();
mousePos.y = event->globalY();
HWND cWindow = GetAncestor(WindowFromPoint(mousePos), GA_ROOT);
mCurrentWindow = (WId) cWindow;
if (mCurrentWindow == winId()) {
mWindowIcon->setPixmap(QPixmap());
mWindowLabel->setText("");
return;
}
// Text
WCHAR str[60];
HICON icon;
::GetWindowText((HWND)mCurrentWindow, str, 60);
windowName = QString::fromWCharArray(str);
///
// Retrieving the application icon
icon = (HICON)::GetClassLong((HWND)mCurrentWindow, GCL_HICON);
if (icon != NULL) {
mWindowIcon->setPixmap(QtWin::fromHICON(icon));
} else {
mWindowIcon->setPixmap(QPixmap());
}
#elif defined(Q_OS_LINUX)
Window cWindow = os::windowUnderCursor(false);
if (cWindow == mCurrentWindow) {
return;
}
mCurrentWindow = cWindow;
if (mCurrentWindow == winId()) {
mWindowIcon->setPixmap(QPixmap());
mWindowLabel->setText("");
return;
}
// Getting the window name property.
XTextProperty tp;
char **text;
int count;
if (XGetTextProperty(QX11Info::display(), cWindow, &tp, XA_WM_NAME) != 0 && tp.value != NULL) {
if (tp.encoding == XA_STRING) {
windowName = QString::fromLocal8Bit((const char *) tp.value);
} else if (XmbTextPropertyToTextList(QX11Info::display(), &tp, &text, &count) == Success &&
text != NULL && count > 0) {
windowName = QString::fromLocal8Bit(text[0]);
XFreeStringList(text);
}
XFree(tp.value);
}
// Retrieving the _NET_WM_ICON property.
Atom type_ret = None;
unsigned char *data = 0;
int format = 0;
unsigned long n = 0;
unsigned long extra = 0;
int width = 0;
int height = 0;
Atom _net_wm_icon = XInternAtom(QX11Info::display(), "_NET_WM_ICON", False);
if (XGetWindowProperty(QX11Info::display(), cWindow, _net_wm_icon, 0, 1, False,
XA_CARDINAL, &type_ret, &format, &n, &extra, (unsigned char **)&data) == Success && data) {
width = data[0];
XFree(data);
}
if (XGetWindowProperty(QX11Info::display(), cWindow, _net_wm_icon, 1, 1, False,
XA_CARDINAL, &type_ret, &format, &n, &extra, (unsigned char **)&data) == Success && data) {
height = data[0];
XFree(data);
}
if (XGetWindowProperty(QX11Info::display(), cWindow, _net_wm_icon, 2, width * height, False,
XA_CARDINAL, &type_ret, &format, &n, &extra, (unsigned char **)&data) == Success && data) {
QImage img(data, width, height, QImage::Format_ARGB32);
mWindowIcon->setPixmap(QPixmap::fromImage(img));
XFree(data);
} else {
mWindowIcon->setPixmap(QPixmap());
}
#endif
QString windowText;
if (!mWindowIcon->pixmap()) {
windowText = QString(" - %1").arg(windowName);
} else {
windowText = windowName;
}
if (windowText == " - ") {
mWindowLabel->setText("");
return;
}
if (windowText.length() == 62) {
mWindowLabel->setText(windowText + "...");
} else {
mWindowLabel->setText(windowText);
}
}
void WindowPicker::mousePressEvent(QMouseEvent *event)
{
qApp->setOverrideCursor(QCursor(mCrosshair));
mCrosshairLabel->setMinimumWidth(mCrosshairLabel->width());
mCrosshairLabel->setMinimumHeight(mCrosshairLabel->height());
mCrosshairLabel->setPixmap(QPixmap());
QWidget::mousePressEvent(event);
}
void WindowPicker::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
#if defined(Q_OS_WIN)
POINT mousePos;
mousePos.x = event->globalX();
mousePos.y = event->globalY();
HWND nativeWindow = GetAncestor(WindowFromPoint(mousePos), GA_ROOT);
#elif defined(Q_OS_LINUX)
Window nativeWindow = os::windowUnderCursor(false);
#endif
if ((WId)nativeWindow == winId()) {
cancel();
return;
}
mTaken = true;
setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
close();
#ifdef Q_OS_LINUX
emit pixmap(QPixmap::grabWindow(mCurrentWindow));
#else
emit pixmap(os::grabWindow((WId)nativeWindow));
#endif
return;
}
close();
}
diff --git a/widgets/hotkeywidget.cpp b/widgets/hotkeywidget.cpp
index 4d982e3..e33f246 100644
--- a/widgets/hotkeywidget.cpp
+++ b/widgets/hotkeywidget.cpp
@@ -1,162 +1,162 @@
/*
* Copyright (C) 2017 Christian Kaiser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QApplication>
#include <QFocusEvent>
#include <QKeyEvent>
#include <QKeySequence>
#include <QStyle>
#include <QTimer>
#include "hotkeywidget.h"
HotkeyWidget::HotkeyWidget(QWidget *parent) :
QPushButton(parent), mHotkey(QKeySequence()), mShowingError(false), mKeyboardFocus(false)
{
mDefaultStyleSheet = "text-align: left; padding: 3px 6px;";
setStyleSheet(mDefaultStyleSheet);
setText(tr("Click to select hotkey..."));
setObjectName("HotkeyWidget");
if (qApp->style()->objectName() == "oxygen") {
setMinimumWidth(130);
} else {
setMinimumWidth(110);
}
}
void HotkeyWidget::setHotkey(const QString &hotkeyString)
{
mHotkey = QKeySequence().fromString(hotkeyString, QKeySequence::NativeText);
setHotkeyText();
}
QString HotkeyWidget::hotkey() const
{
return mHotkey.toString(QKeySequence::PortableText);
}
void HotkeyWidget::showError()
{
if (mShowingError) {
return;
}
mShowingError = true;
setStyleSheet(mDefaultStyleSheet + "color: #d90000;");
- QTimer::singleShot(1000, this, SLOT(hideError()));
+ QTimer::singleShot(1000, this, &HotkeyWidget::hideError);
}
void HotkeyWidget::setHotkeyText()
{
QString hotkeyText = mHotkey.toString(QKeySequence::NativeText);
setText(hotkeyText);
parentWidget()->setFocus();
}
bool HotkeyWidget::event(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
setHotkeyText();
} else if (event->type() == QEvent::KeyPress) {
keyPressEvent(static_cast<QKeyEvent *>(event));
return true;
} else if (event->type() == QEvent::FocusIn) {
QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
if (focusEvent->reason() != Qt::TabFocusReason) {
setText(tr("Type your hotkey"));
mKeyboardFocus = false;
grabKeyboard();
} else {
mKeyboardFocus = true;
}
} else if (event->type() == QEvent::FocusOut) {
if (text() == tr("Invalid hotkey")) {
emit invalidHotkeyError();
showError();
}
releaseKeyboard();
setHotkeyText(); // Reset the text
} else if ((event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride || event->type() == QEvent::Shortcut) && hasFocus()) {
event->accept();
return true;
}
return QPushButton::event(event);
}
void HotkeyWidget::keyPressEvent(QKeyEvent *event)
{
if (mKeyboardFocus) {
return;
}
if (isModifier(event->key())) {
return;
}
if (!isValid(event->key())) {
setText(tr("Invalid hotkey"));
parentWidget()->setFocus();
return;
}
mHotkey = QKeySequence(event->key() + (event->modifiers() & ~Qt::KeypadModifier));
setHotkeyText();
}
void HotkeyWidget::hideError()
{
setStyleSheet(mDefaultStyleSheet);
mShowingError = false;
}
bool HotkeyWidget::isValid(int key) const
{
switch (key) {
case 0:
case Qt::Key_Escape:
case Qt::Key_unknown:
return false;
}
return !isModifier(key);
}
bool HotkeyWidget::isModifier(int key) const
{
switch (key) {
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Meta:
case Qt::Key_Alt:
case Qt::Key_AltGr:
case Qt::Key_Super_L:
case Qt::Key_Super_R:
case Qt::Key_Menu:
return true;
}
return false;
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, May 15, 7:40 PM (21 m, 51 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
63970
Default Alt Text
(88 KB)

Event Timeline