Hotkeys and crop improvements

This commit is contained in:
ArsenArsen 2017-04-26 22:00:13 +02:00
parent 3877799895
commit 2ef75d7576
13 changed files with 189 additions and 21 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "QHotkey"]
path = QHotkey
url = https://github.com/Skycoder42/QHotkey.git

View File

@ -37,7 +37,9 @@ SOURCES += main.cpp\
uploaders/default/clipboarduploader.cpp \
formatter.cpp \
uploaders/customuploader.cpp \
notifications.cpp
notifications.cpp \
hotkeying.cpp \
sequencedialog.cpp
HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \
@ -53,7 +55,9 @@ HEADERS += mainwindow.hpp \
uploaders/default/clipboarduploader.hpp \
formatter.hpp \
uploaders/customuploader.hpp \
notifications.hpp
notifications.hpp \
hotkeying.hpp \
sequencedialog.hpp
FORMS += mainwindow.ui
@ -68,3 +72,5 @@ ICON = icons/favicon.ico
# Enable debug symbols
QMAKE_CFLAGS_DEBUG += -g
include(QHotkey/qhotkey.pri)

1
QHotkey Submodule

@ -0,0 +1 @@
Subproject commit 040cf5ec0cf2ed605c39d7bc4d0164ab7c54d6bf

View File

@ -7,22 +7,27 @@ A [ShareX](https://github.com/ShareX/) clone written in Qt. Should be cross plat
* Qt 5 Widgets
* Qt 5 GUI
* Qt 5 Network
* (QHotkey)[https://github.com/Skycoder42/QHotkey]
Despite the name implying so, this project does not depend on the KDE API at all.
## Goals
* Same support for Windows, Linux and Mac (if I ever get testers)
* Screenshotting:
* 1. Fullscreen,
* 2. Area;
* 1. Fullscreen, [target: 1.0] [done]
* 2. Area; [target: 1.0] [done]
* Screen recording, same options as above:
* 1. WebM
* 2. GIF (nopls)
* Custom uploader support
* Custom uploader support [target: 1.0] [done]
* Default uploaders, including:
* 1. imgur
* 2. Clipboard (not an uploader)
* 1. imgur [target: 1.0] [done]
* 2. Clipboard (not an uploader) [target: 1.0] [done]
* 3. (S)FTP
* Oh, and a good icon.
* Oh, and a good icon. [looks good to me, but to noone else]
## Wayland Support
If it's requested enough, I will make a pull request towards QHotkey to support Wayland. Or open an issue, depending on my mood. But Qt _should_ still work on Wayland.
###### Started on 19th of April 2017 to bring some attention and improvement to Linux screenshotting.

View File

@ -13,6 +13,7 @@ CropEditor::CropEditor(QPixmap *image, QObject *parent) : QObject(parent)
view = new CropView(scene);
pixmapItem = new QGraphicsPixmapItem(*pixmap);
pixmapItem->setZValue(-1);
scene->addItem(pixmapItem);
scene->setSceneRect(pixmap->rect());

View File

@ -1,9 +1,22 @@
#include "cropscene.hpp"
#include <QDebug>
#include <QGraphicsPolygonItem>
#include <QGraphicsView>
#include <QTimer>
CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt::NoButton)
{
QTimer::singleShot(0, [&] {
QPolygonF poly;
poly.append(sceneRect().topLeft());
poly.append(sceneRect().topRight());
poly.append(sceneRect().bottomRight());
poly.append(sceneRect().bottomLeft());
polyItem = new QGraphicsPolygonItem(poly);
polyItem->setBrush(QBrush(QColor(0, 0, 0, 191)));
polyItem->setPen(QPen(Qt::NoPen));
addItem(polyItem);
});
}
CropScene::~CropScene()
@ -35,10 +48,26 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
}
else
{
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()),
qAbs(initPos.x() - p.x()), qAbs(initPos.y() - p.y())));
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()),
qAbs(initPos.y() - p.y())));
}
}
QPolygonF poly;
QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom());
poly << sceneRect().topLeft();
poly << sceneRect().topRight();
poly << sceneRect().bottomRight();
poly << theMagicWikipediaPoint;
poly << rect->rect().bottomRight();
poly << rect->rect().topRight();
poly << rect->rect().topLeft();
poly << rect->rect().bottomLeft();
poly << rect->rect().bottomRight();
poly << theMagicWikipediaPoint;
poly << sceneRect().bottomLeft();
poly << sceneRect().topLeft();
this->polyItem->setPolygon(poly);
e->accept();
}
else

View File

@ -27,6 +27,7 @@ class CropScene : public QGraphicsScene
QFlags<Qt::MouseButton> prevButtons;
QGraphicsRectItem *rect = nullptr;
QPointF initPos;
QGraphicsPolygonItem *polyItem = nullptr;
};
#endif // CROPSCENE_HPP

43
hotkeying.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "hotkeying.hpp"
#include <QHotkey>
#include <QMap>
#include <settings.hpp>
QMap<QString, QHotkey *> hotkeys;
// func gets bound only on first set, or load
void hotkeying::hotkey(QString seqName, QKeySequence seq, std::function<void()> func)
{
if (hotkeys.contains(seqName))
hotkeys.value(seqName)->setShortcut(seq, true);
else
{
QHotkey *hotkey = new QHotkey(seq, true);
QObject::connect(hotkey, &QHotkey::activated, func);
hotkeys.insert(seqName, hotkey);
}
settings::settings().setValue(seqName.prepend("hotkey_"), seq.toString());
}
// forces the hotkey from settings
void hotkeying::load(QString seqName, std::function<void()> func)
{
QHotkey *h;
if (settings::settings().contains(seqName.prepend("hotkey_")))
h = new QHotkey(QKeySequence(settings::settings().value(seqName.prepend("hotkey_")).toString()), true);
else
h = new QHotkey;
QObject::connect(h, &QHotkey::activated, func);
hotkeys.insert(seqName, h);
}
bool hotkeying::valid(QString seq)
{
return !QKeySequence(seq).toString().isEmpty();
}
QString hotkeying::sequence(QString seqName)
{
return hotkeys.contains(seqName) ? hotkeys.value(seqName)->shortcut().toString() : "";
}

16
hotkeying.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef HOTKEYING_HPP
#define HOTKEYING_HPP
#include <QKeySequence>
#include <QString>
#include <functional>
namespace hotkeying
{
void hotkey(QString seqName, QKeySequence seq, std::function<void()> func);
bool valid(QString seq);
void load(QString seqName, std::function<void()> func);
QString sequence(QString seqName);
}
#endif // HOTKEYING_HPP

View File

@ -1,4 +1,5 @@
#include "mainwindow.hpp"
#include "sequencedialog.hpp"
#include <QApplication>
int main(int argc, char *argv[])
@ -7,6 +8,7 @@ int main(int argc, char *argv[])
a.setApplicationName("KShare");
a.setOrganizationName("ArsenArsen");
a.setApplicationVersion("1.0");
MainWindow w;
w.show();
return a.exec();

View File

@ -11,11 +11,23 @@
#include <QStatusBar>
#include <QSystemTrayIcon>
#include <QTimer>
#include <functional>
#include <hotkeying.hpp>
#include <sequencedialog.hpp>
#include <settings.hpp>
#include <uploaders/uploadersingleton.hpp>
MainWindow *MainWindow::instance;
void addHotkeyItem(QString text, QString name, std::function<void()> *func)
{
QListWidgetItem *item = new QListWidgetItem(text, MainWindow::inst()->ui->hotkeys);
item->setData(Qt::UserRole + 1, name);
MainWindow::inst()->fncs.insert(name, func);
hotkeying::load(name, *func);
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
instance = this;
@ -62,6 +74,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
ui->delay->setValue(settings::settings().value("delay").toDouble());
else
ui->delay->setValue(0.25);
// keys are hot, wait what
hotkeying::load("fullscreen", [this] { on_actionFullscreen_triggered(); });
hotkeying::load("area", [this] { on_actionArea_triggered(); });
ui->hotkeys->setSelectionMode(QListWidget::SingleSelection);
addHotkeyItem("Fullscreen image", "fullscreen", new std::function<void()>([&] { on_actionFullscreen_triggered(); }));
addHotkeyItem("Area image", "area", new std::function<void()>([&] { on_actionArea_triggered(); }));
}
MainWindow::~MainWindow()
@ -141,3 +162,15 @@ void MainWindow::on_delay_valueChanged(double arg1)
{
settings::settings().setValue("delay", arg1);
}
void MainWindow::on_hotkeys_doubleClicked(const QModelIndex &)
{
if (ui->hotkeys->selectedItems().length() == 1)
{
QListWidgetItem *i = ui->hotkeys->selectedItems().at(0);
QString str = i->data(Qt::UserRole + 1).toString();
QString seq = QInputDialog::getText(ui->centralWidget, "Hotkey Input", "Insert hotkey:", QLineEdit::Normal,
hotkeying::sequence(str));
if (hotkeying::valid(seq)) hotkeying::hotkey(str, QKeySequence(seq), *fncs.value(str));
}
}

View File

@ -3,7 +3,9 @@
#include <QDoubleSpinBox>
#include <QMainWindow>
#include <QMap>
#include <QSystemTrayIcon>
#include <functional>
#include <uploaders/uploader.hpp>
@ -28,18 +30,21 @@ class MainWindow : public QMainWindow
void on_delay_valueChanged(double arg1);
void on_hotkeys_doubleClicked(const QModelIndex &index);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
QSystemTrayIcon *tray;
void setScheme(QString scheme);
QDoubleSpinBox *delay();
static MainWindow *inst();
QMap<QString, std::function<void()> *> fncs;
private:
Ui::MainWindow *ui;
static MainWindow *instance;
protected:

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<height>315</height>
<width>512</width>
<height>353</height>
</rect>
</property>
<property name="windowTitle">
@ -56,13 +56,6 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>s</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QListWidget" name="uploaderList"/>
</item>
@ -76,6 +69,36 @@
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>s</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Hotkeys</string>
</property>
</widget>
</item>
<item row="1" column="2" rowspan="5">
<widget class="QListWidget" name="hotkeys"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
@ -83,7 +106,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<width>512</width>
<height>24</height>
</rect>
</property>