Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
18 KB
Referenced Files
None
Subscribers
None
diff --git a/dialogs/previewdialog.cpp b/dialogs/previewdialog.cpp
index 420d43e..d6ef2e3 100644
--- a/dialogs/previewdialog.cpp
+++ b/dialogs/previewdialog.cpp
@@ -1,436 +1,436 @@
/*
* Copyright (C) 2016 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"));
QSettings *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();
}
QHBoxLayout *l = new QHBoxLayout;
mStack = new QStackedLayout;
connect(mStack, SIGNAL(currentChanged(int)), this, SLOT(indexChanged(int)));
mPrevButton = new QPushButton(os::icon("arrow-left"), "", this);
connect(mPrevButton, SIGNAL(clicked()), this, SLOT(previous()));
mNextButton = new QPushButton(os::icon("arrow-right"), "", this);
connect(mNextButton, SIGNAL(clicked()), this, SLOT(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);
l->addWidget(mPrevButton);
l->addLayout(mStack);
l->addWidget(mNextButton);
l->setMargin(0);
l->setContentsMargins(6, 6, 6, 6);
mStack->setMargin(0);
setMaximumHeight(mSize);
setLayout(l);
if (mAutoclose) {
startTimer(1000);
}
}
void PreviewDialog::add(Screenshot *screenshot)
{
if (!isVisible()) {
show();
}
if (mAutoclose) {
mAutoclose = mAutocloseReset;
}
QLabel *label = new QLabel(this);
label->setGraphicsEffect(os::shadow());
bool small = false;
QSize size = screenshot->pixmap().size();
if (size.width() > mSize || size.height() > mSize) {
size.scale(mSize, mSize, Qt::KeepAspectRatio);
} else {
small = 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, SIGNAL(triggered()), screenshot, SLOT(markUpload()));
connect(uploadAction, SIGNAL(triggered()), screenshot, SLOT(confirm()));
connect(uploadAction, SIGNAL(triggered()), this, SLOT(closePreview()));
connect(this, SIGNAL(uploadAll()), uploadAction, SLOT(trigger()));
confirmMenu->addAction(uploadAction);
confirmPushButton->setMenu(confirmMenu);
confirmPushButton->setPopupMode(QToolButton::MenuButtonPopup);
}
connect(this, SIGNAL(acceptAll()), confirmPushButton, SLOT(click()));
connect(confirmPushButton, SIGNAL(clicked()), screenshot, SLOT(confirm()));
connect(confirmPushButton, SIGNAL(clicked()), this, SLOT(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, SIGNAL(acceptAll()), confirmAction, SLOT(trigger()));
connect(confirmAction, SIGNAL(triggered()), screenshot, SLOT(confirm()));
connect(confirmAction, SIGNAL(triggered()), this, SLOT(closePreview()));
connect(confirmPushButton, SIGNAL(clicked()), screenshot, SLOT(markUpload()));
connect(confirmPushButton, SIGNAL(clicked()), screenshot, SLOT(confirm()));
connect(confirmPushButton, SIGNAL(clicked()), this, SLOT(closePreview()));
connect(this, SIGNAL(uploadAll()), confirmPushButton, SLOT(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(small);
connect(this, SIGNAL(rejectAll()), discardPushButton, SLOT(click()));
connect(discardPushButton, SIGNAL(clicked()), screenshot, SLOT(discard()));
connect(discardPushButton, SIGNAL(clicked()), this, SLOT(closePreview()));
connect(enlargePushButton, SIGNAL(clicked()), this, SLOT(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);
+ 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/main.cpp b/main.cpp
index d941d0d..a3f72dc 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,93 +1,94 @@
/*
* Copyright (C) 2016 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 <QLocale>
#ifdef Q_OS_WIN
#include <QtWinExtras>
#endif
#include "tools/os.h"
#include "tools/SingleApplication/singleapplication.h"
#include "lightscreenwindow.h"
int main(int argc, char *argv[])
{
#ifdef QT_DEBUG
qSetMessagePattern("%{message} @%{line}[%{function}()]");
#endif
QApplication::setOrganizationName("K");
QApplication::setApplicationName("Lightscreen");
QApplication::setApplicationVersion(APP_VERSION);
+ QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
SingleApplication application(argc, argv);
application.setQuitOnLastWindowClosed(false);
LightscreenWindow lightscreen;
#ifdef Q_OS_WIN
if (QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS7) {
QWinJumpList *jumplist = new QWinJumpList(&lightscreen);
QColor backgroundColor = qApp->palette("QToolTip").color(QPalette::Background);
if (QSysInfo::WindowsVersion == QSysInfo::WV_WINDOWS10) {
// contrast r hard
backgroundColor = Qt::black;
}
QWinJumpListCategory *screenshotCategory = new QWinJumpListCategory("Screenshot");
screenshotCategory->setVisible(true);
screenshotCategory->addLink(os::icon("screen", backgroundColor), QObject::tr("Screen") , application.applicationFilePath(), QStringList("--screen"));
screenshotCategory->addLink(os::icon("area", backgroundColor), QObject::tr("Area") , application.applicationFilePath(), QStringList("--area"));
screenshotCategory->addLink(os::icon("pickWindow", backgroundColor), QObject::tr("Pick Window") , application.applicationFilePath(), QStringList("--pickwindow"));
QWinJumpListCategory *uploadCategory = new QWinJumpListCategory("Upload");
uploadCategory->setVisible(true);
uploadCategory->addLink(os::icon("imgur", backgroundColor), QObject::tr("Upload Last") , application.applicationFilePath(), QStringList("--uploadlast"));
uploadCategory->addLink(os::icon("view-history", backgroundColor), QObject::tr("View History") , application.applicationFilePath(), QStringList("--viewhistory"));
QWinJumpListCategory *actionsCategory = new QWinJumpListCategory("Actions");
actionsCategory->setVisible(true);
actionsCategory->addLink(os::icon("configure", backgroundColor), QObject::tr("Options") , application.applicationFilePath(), QStringList("--options"));
actionsCategory->addLink(os::icon("folder", backgroundColor), QObject::tr("Go to Folder") , application.applicationFilePath(), QStringList("--folder"));
actionsCategory->addLink(os::icon("no.big", backgroundColor), QObject::tr("Quit Lightscreen") , application.applicationFilePath(), QStringList("--quit"));
jumplist->addCategory(screenshotCategory);
jumplist->addCategory(uploadCategory);
jumplist->addCategory(actionsCategory);
}
#endif
if (application.arguments().size() > 1) {
lightscreen.executeArguments(application.arguments());
} else {
lightscreen.show();
}
QObject::connect(&application, SIGNAL(instanceArguments(const QStringList)), &lightscreen, SLOT(executeArguments(const QStringList)));
QObject::connect(&lightscreen, SIGNAL(finished()), &application, SLOT(quit()));
int result = application.exec();
return result;
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Jun 17, 9:06 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70601
Default Alt Text
(18 KB)

Event Timeline