KShare/mainwindow.cpp

181 lines
5.4 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "mainwindow.hpp"
#include "screenshotter.hpp"
#include "screenshotutil.hpp"
#include "ui_mainwindow.h"
#include <QAction>
#include <QCloseEvent>
#include <QCoreApplication>
2017-04-25 22:36:33 +02:00
#include <QDoubleSpinBox>
2017-04-26 23:26:00 +02:00
#include <QInputDialog>
2017-04-23 15:05:48 +02:00
#include <QListWidgetItem>
#include <QMenu>
2017-04-25 16:04:46 +02:00
#include <QStatusBar>
2017-04-23 15:05:48 +02:00
#include <QSystemTrayIcon>
#include <QTimer>
2017-04-26 22:00:13 +02:00
#include <functional>
#include <hotkeying.hpp>
2017-04-23 20:29:24 +02:00
#include <settings.hpp>
2017-04-23 15:05:48 +02:00
#include <uploaders/uploadersingleton.hpp>
2017-04-25 22:17:36 +02:00
MainWindow *MainWindow::instance;
2017-04-26 22:00:13 +02:00
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);
}
2017-04-23 15:05:48 +02:00
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
2017-04-25 22:17:36 +02:00
instance = this;
2017-04-23 15:05:48 +02:00
ui->setupUi(this);
2017-04-27 18:58:23 +02:00
setWindowIcon(QIcon(":/icons/icon.png"));
2017-04-23 15:05:48 +02:00
tray = new QSystemTrayIcon(windowIcon(), this);
tray->setToolTip("KShare");
tray->setVisible(true);
QMenu *menu = new QMenu(this);
QAction *quit = new QAction("Quit", this);
QAction *shtoggle = new QAction("Show/Hide", this);
QAction *fullscreen = new QAction("Take fullscreen shot", this);
QAction *area = new QAction("Take area shot", this);
menu->addActions({ quit, shtoggle });
menu->addSeparator();
menu->addActions({ fullscreen, area });
connect(quit, &QAction::triggered, this, &MainWindow::quit);
connect(shtoggle, &QAction::triggered, this, &MainWindow::toggleVisible);
2017-04-28 16:35:53 +02:00
connect(tray, &QSystemTrayIcon::messageClicked, this, &MainWindow::toggleVisible);
connect(tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::DoubleClick) toggleVisible();
});
connect(fullscreen, &QAction::triggered, this, [] { screenshotter::fullscreenDelayed(); });
connect(area, &QAction::triggered, this, [] { screenshotter::areaDelayed(); });
2017-04-23 15:05:48 +02:00
tray->setContextMenu(menu);
ui->uploaderList->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->uploaderList->setSelectionMode(QAbstractItemView::SingleSelection);
// Add items to uploader selection
2017-04-25 21:27:29 +02:00
for (Uploader *u : UploaderSingleton::inst().uploaderList()) newUploader(u);
connect(&UploaderSingleton::inst(), &UploaderSingleton::newUploader, this, &MainWindow::newUploader);
2017-04-23 20:29:24 +02:00
// Set filename scheme
if ((settings::settings().contains("fileFormat")))
setScheme(settings::settings().value("fileFormat").toString());
else
setScheme("Screenshot %(yyyy-MM-dd HH:mm:ss)date");
2017-04-25 16:04:46 +02:00
auto errors = UploaderSingleton::inst().errors();
if (errors.length() == 1)
statusBar()->showMessage(errors.at(0).what());
else
2017-04-25 22:17:36 +02:00
statusBar()->showMessage(QString("Errors visible in console (if present). Count: " + QString::number(errors.size())));
2017-04-25 22:36:33 +02:00
// Set delay
if ((settings::settings().contains("delay")))
ui->delay->setValue(settings::settings().value("delay").toDouble());
else
ui->delay->setValue(0.25);
2017-04-26 22:00:13 +02:00
ui->hotkeys->setSelectionMode(QListWidget::SingleSelection);
2017-04-29 09:44:30 +02:00
addHotkeyItem("Fullscreen image", "fullscreen", new std::function<void()>([] { screenshotter::fullscreen(); }));
addHotkeyItem("Area image", "area", new std::function<void()>([] { screenshotter::area(); }));
2017-04-23 15:05:48 +02:00
}
MainWindow::~MainWindow()
{
delete ui;
}
2017-04-23 20:29:24 +02:00
void MainWindow::setScheme(QString scheme)
{
ui->nameScheme->setText(scheme);
}
2017-04-25 22:36:33 +02:00
QDoubleSpinBox *MainWindow::delay()
{
return ui->delay;
}
2017-04-25 22:17:36 +02:00
MainWindow *MainWindow::inst()
{
return instance;
}
2017-04-23 15:05:48 +02:00
void MainWindow::closeEvent(QCloseEvent *event)
{
event->ignore();
QTimer::singleShot(0, this, &MainWindow::hide);
}
void MainWindow::quit()
{
QCoreApplication::quit();
}
void MainWindow::toggleVisible()
{
this->setVisible(!this->isVisible());
2017-04-30 11:37:12 +02:00
if (this->isVisible())
{
this->raise();
}
2017-04-23 15:05:48 +02:00
}
2017-04-25 21:27:29 +02:00
void MainWindow::newUploader(Uploader *u)
{
QListWidgetItem *item = new QListWidgetItem(u->name());
item->setToolTip(u->description());
ui->uploaderList->addItem(item);
if (u->name() == UploaderSingleton::inst().selectedUploader()) item->setSelected(true);
}
2017-04-23 15:05:48 +02:00
void MainWindow::on_actionQuit_triggered()
{
quit();
}
void MainWindow::on_actionFullscreen_triggered()
{
2017-04-25 22:36:33 +02:00
screenshotter::fullscreenDelayed();
2017-04-23 15:05:48 +02:00
}
void MainWindow::on_actionArea_triggered()
{
2017-04-25 22:36:33 +02:00
screenshotter::areaDelayed();
2017-04-23 15:05:48 +02:00
}
void MainWindow::on_uploaderList_clicked(const QModelIndex &)
{
QList<QListWidgetItem *> index = ui->uploaderList->selectedItems();
if (index.size() == 1)
{
UploaderSingleton::inst().set(index.at(0)->text());
}
}
2017-04-23 20:29:24 +02:00
void MainWindow::on_nameScheme_textEdited(const QString &arg1)
{
settings::settings().setValue("fileFormat", arg1);
}
2017-04-25 22:36:33 +02:00
void MainWindow::on_delay_valueChanged(double arg1)
{
settings::settings().setValue("delay", arg1);
}
2017-04-26 22:00:13 +02:00
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));
}
}