Browse Source

added upload from clipboard

tags/v5.0.0^2
Niklas 3 years ago
parent
commit
b5b54cefd8
  1. 1
      docs/index.md
  2. 68
      src/clipboard/clipboardcopy.cpp
  3. 11
      src/clipboard/clipboardcopy.hpp
  4. 4
      src/mainwindow.cpp
  5. 2
      src/settingsdialog.cpp
  6. 6
      src/src.pro
  7. 14
      src/uploaders/uploadersingleton.cpp
  8. 1
      src/uploaders/uploadersingleton.hpp

1
docs/index.md

@ -19,6 +19,7 @@ It has many features, including:
* Hotkeys,
* Color picker, and last but not least,
* Custom upload destinations
* Upload from clipboard
## Enough talking, show us how it looks
The main window is rather simple, with only a log, and a button in it:

68
src/clipboard/clipboardcopy.cpp

@ -0,0 +1,68 @@
#include "clipboardcopy.hpp"
#include <QClipboard>
#include <QMimeData>
#include <QClipboard>
#include <QApplication>
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QMimeDatabase>
#include <QPixmap>
#include <QTemporaryFile>
#include <QIODevice>
#include <QTextStream>
#include <QMediaPlayer>
#include <QUrl>
#include <logger.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <uploaders/uploadersingleton.hpp>
#include "mainwindow.hpp"
void clipboardcopy::copyClipboard() {
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
if(mimeData->hasImage()) {
QPixmap map = qvariant_cast<QPixmap>(mimeData->imageData());
} else if(mimeData->hasText()) {
QFileInfo fileInfo(mimeData->text());
if(fileInfo.exists() && fileInfo.isReadable() && fileInfo.isFile()) {
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile(fileInfo);
QString type = mimeType.name();
QFile file(fileInfo.absoluteFilePath());
logger::info(type);
UploaderSingleton::inst().upload(file);
} else if (fileInfo.exists() && fileInfo.isReadable() && fileInfo.isDir()) {
notifications::notify("KShare - Directory is not uploadable", fileInfo.absolutePath(), QSystemTrayIcon::Warning);
playErrorSound();
} else {
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
if(tmpFile.open()) {
QTextStream stream(&tmpFile);
stream << mimeData->text();
stream.flush();
tmpFile.seek(0);
UploaderSingleton::inst().upload(tmpFile);
} else {
logger::warn("Can not open tmp file");
playErrorSound();
}
}
} else {
notifications::notify("Unsupported File Format", "Can not upload clipboard", QSystemTrayIcon::Warning);
playErrorSound();
}
}
void clipboardcopy::playErrorSound() {
QMediaPlayer*mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/errorsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

11
src/clipboard/clipboardcopy.hpp

@ -0,0 +1,11 @@
#ifndef KSHARE_CLIPBOARDCOPY_HPP
#define KSHARE_CLIPBOARDCOPY_HPP
namespace clipboardcopy {
void copyClipboard();
void playErrorSound();
}
#endif //KSHARE_CLIPBOARDCOPY_HPP

4
src/mainwindow.cpp

@ -25,6 +25,7 @@
#include <logs/requestlogging.hpp>
#include "io/ioutils.hpp"
#include <monospacetextdialog.hpp>
#include <clipboard/clipboardcopy.hpp>
MainWindow *MainWindow::instance;
@ -112,7 +113,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
connect(ui->areaButton, &QPushButton::clicked, this, [] { screenshotter::areaDelayed(); });
connect(ui->aboutButton, &QPushButton::clicked, this, &MainWindow::on_actionAbout_triggered);
connect(ui->screenshotFolderButton, &QPushButton::clicked, this, &MainWindow::openScreenshotFolder);
connect(ui->clipboardButton, &QPushButton::clicked, this, &MainWindow::openScreenshotFolder);
connect(ui->clipboardButton, &QPushButton::clicked, this, &clipboardcopy::copyClipboard);
connect(ui->colorPickerButton, &QPushButton::clicked, this, [] { ColorPickerScene::showPicker(); });
ui->treeWidget->addAction(ui->actionOpenURL);
@ -128,6 +129,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
addHotkey("area", [] { screenshotter::area(); });
addHotkey("active", [] { screenshotter::active(); });
addHotkey("picker", [] { ColorPickerScene::showPicker(); });
addHotkey("clipboard", [] { clipboardcopy::copyClipboard(); });
addHotkey("recordingstop", [&] { controller->end(); });
addHotkey("recordingabort", [&] { controller->abort(); });
addHotkey("recordingstart", [&] { this->rec(); });

2
src/settingsdialog.cpp

@ -19,6 +19,7 @@
#include <screenshotter.hpp>
#include <settings.hpp>
#include <uploaders/uploadersingleton.hpp>
#include <clipboard/clipboardcopy.hpp>
static QMap<QString, std::function<void()>> fncs;
@ -57,6 +58,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::Se
#ifdef PLATFORM_CAPABILITY_ACTIVEWINDOW
addHotkeyItem(ui->hotkeys, tr("Active window"), "active", [&] { screenshotter::active(); });
#endif
addHotkeyItem(ui->hotkeys, tr("Copy Clipboard"), "clipboard", [] { clipboardcopy::copyClipboard(); });
addHotkeyItem(ui->hotkeys, tr("Color picker"), "picker", [] { ColorPickerScene::showPicker(); });
addHotkeyItem(ui->hotkeys, tr("Stop Recording"), "recordingstop", [&] { MainWindow::inst()->controller->end(); });
addHotkeyItem(ui->hotkeys, tr("Start Recording"), "recordingstart", [&] { MainWindow::inst()->rec(); });

6
src/src.pro

@ -72,7 +72,8 @@ SOURCES += main.cpp\
screenoverlay/screenoverlayview.cpp \
screenoverlay/screenoverlay.cpp \
screenoverlay/screenoverlaysettings.cpp \
logger.cpp
logger.cpp \
clipboard/clipboardcopy.cpp
HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \
@ -123,7 +124,8 @@ HEADERS += mainwindow.hpp \
screenoverlay/screenoverlayview.hpp \
screenoverlay/screenoverlay.hpp \
screenoverlay/screenoverlaysettings.hpp \
logger.hpp
logger.hpp \
clipboard/clipboardcopy.hpp
nopkg {
# win32 {

14
src/uploaders/uploadersingleton.cpp

@ -105,8 +105,8 @@ void UploaderSingleton::upload(QFile &img, QString format) {
updateSaveSettings();
if (img.size() <= 0) return;
if (!saveImages || img.rename(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) {
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) {
playSound();
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
@ -117,6 +117,16 @@ void UploaderSingleton::upload(QFile &img, QString format) {
notifications::notify(tr("KShare - Failed to save picture"), img.errorString(), QSystemTrayIcon::Warning);
}
void UploaderSingleton::upload(QFile &img) {
updateSaveSettings();
if (img.size() <= 0) return;
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), "", fileInfo.fileName());
else
notifications::notify(tr("KShare - Failed to open File"), img.errorString(), QSystemTrayIcon::Warning);
}
void UploaderSingleton::showSettings() {
uploaders.value(uploader)->showSettings();
}

1
src/uploaders/uploadersingleton.hpp

@ -17,6 +17,7 @@ public:
void upload(QPixmap pixmap);
void upload(QByteArray img, QString format);
void upload(QFile &img, QString format);
void upload(QFile &img);
void showSettings();
QList<Uploader *> uploaderList();
void set(QString uploader);

Loading…
Cancel
Save