Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7789cc9

Browse files
committed
Start of clipboard monitoring
1 parent 23f7447 commit 7789cc9

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

ImageWindow.cpp

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ ImageWindow::ImageWindow(int ID)
7676
scene = nullptr;
7777
colormap = new Colormapper();
7878
dipFactor = 1;
79+
clipboardTimerId = 0;
80+
lastClipboardText = "";
7981
}
8082

8183
void ImageWindow::resetWheelAccumulator()
@@ -1831,8 +1833,39 @@ void ImageWindow::copyScreenshotToClipboard()
18311833
sendToClipboard(image);
18321834
}
18331835

1834-
#include <QDateTime>
1836+
void ImageWindow::startMonitorClipboard() {
1837+
if (clipboardTimerId != 0)
1838+
return;
1839+
clipboardTimerId = startTimer(1000);
1840+
if (clipboardTimerId == 0)
1841+
cerr << "Could not start timer!" << endl;
1842+
cout << "Started monitoring clipboard." << endl;
1843+
}
1844+
1845+
void ImageWindow::stopMonitorClipboard() {
1846+
if (clipboardTimerId != 0)
1847+
killTimer(clipboardTimerId);
1848+
}
1849+
18351850
#include <QMimeData>
1851+
void ImageWindow::timerEvent(QTimerEvent *event) {
1852+
if (event->timerId() != clipboardTimerId)
1853+
return;
1854+
1855+
QClipboard *clipboard = QApplication::clipboard();
1856+
if (clipboard == nullptr)
1857+
{
1858+
cerr << "Clibpard is null!" << endl;
1859+
return;
1860+
}
1861+
QMimeData const *data = clipboard->mimeData();
1862+
if (data->hasText() && data->text() != lastClipboardText) {
1863+
pasteFromClipboard();
1864+
}
1865+
}
1866+
1867+
1868+
#include <QDateTime>
18361869
#include <QtNetwork>
18371870
#include <QTime>
18381871
bool ImageWindow::pasteFromClipboard()
@@ -1855,46 +1888,56 @@ bool ImageWindow::pasteFromClipboard()
18551888
return false;
18561889
}
18571890
QString filename = QDir::tempPath() + "/temp_" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".png";
1858-
if(cbImage.save(filename))
1891+
if(cbImage.save(filename, "png", 100))
18591892
{
18601893
cout << "Saved clipboard image to " << filename.toStdString() << endl;
1894+
startMonitorClipboard();
18611895
return this->readImage(filename);
18621896
}
18631897
cerr << "Failed to save clipboard image to " << filename.toStdString() << endl;
18641898
return false;
18651899
}
18661900
if (data->hasText()) {
18671901
cout << "Clipboard has text: " << data->text().toStdString() << endl;
1902+
lastClipboardText = data->text();
18681903
auto url = QUrl(data->text());
18691904
bool downloadComplete = false;
18701905
bool downloadFailed = false;
18711906

1907+
cout << "Downloading from " << url.toString().toStdString() << endl;
18721908
QNetworkAccessManager *manager = new QNetworkAccessManager;
18731909
QNetworkRequest request(url);
18741910
QObject::connect(manager, &QNetworkAccessManager::finished, [&](QNetworkReply *reply) {
1911+
QString filename = QDir::tempPath() + "/temp_" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".png";
1912+
QObject::connect(reply, &QNetworkReply::errorOccurred, [&](QNetworkReply::NetworkError code) {
1913+
cerr << "An error occurred: (" << code << ") " << reply->errorString().toStdString() << endl;
1914+
downloadFailed = true;
1915+
});
18751916
QByteArray ba= reply->readAll();
18761917
QImage cbImage;
18771918
if (!cbImage.loadFromData(ba)) {
18781919
cerr << "Failed to download image from " << url.toString().toStdString() << endl;
18791920
downloadFailed = true;
1921+
return;
18801922
}
18811923
cout << "Downloaded " << ba.size() << " bytes from " << url.toString().toStdString() << endl;
1882-
QString filename = QDir::tempPath() + "/temp_" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".png";
1883-
if(cbImage.save(filename))
1924+
if(cbImage.save(filename, "png", 100))
18841925
{
18851926
cout << "Saved clipboard image to " << filename.toStdString() << endl;
18861927
this->readImage(filename);
18871928
downloadComplete = true;
1929+
return;
18881930
} else {
18891931
cerr << "Failed to save clipboard image to " << filename.toStdString() << endl;
18901932
downloadFailed = true;
1933+
return;
18911934
}
18921935
});
18931936
manager->get(request);
18941937
QTime dieTime= QTime::currentTime().addSecs(300);
18951938

18961939
while (!downloadComplete && !downloadFailed && QTime::currentTime() < dieTime){
1897-
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
1940+
QCoreApplication::processEvents(QEventLoop::AllEvents);
18981941
}
18991942
if (!downloadComplete && !downloadFailed) {
19001943
cerr << "Timeout; download took too long." << endl;
@@ -1903,6 +1946,7 @@ bool ImageWindow::pasteFromClipboard()
19031946
if (downloadFailed) {
19041947
return false;
19051948
}
1949+
startMonitorClipboard();
19061950
return true;
19071951
}
19081952
cerr << "Unrecognized clipboard format." << endl;

ImageWindow.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ class ImageWindow : public QGraphicsView
282282
void saveScreenshotToFile();
283283

284284
void setTitle(QString title);
285+
286+
int clipboardTimerId;
287+
QString lastClipboardText;
288+
void startMonitorClipboard();
289+
void stopMonitorClipboard();
290+
void timerEvent(QTimerEvent *event) override;
291+
285292
signals:
286293
// These are connected to slots in MainDialog for inter-window syncing.
287294
// When something happens in an ImageWindow, these signals notify the main window.

definitions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef DEFINITIONS_H
22
#define DEFINITIONS_H
33

4-
#define VERSION_STRING "2021-06-02"
4+
#define VERSION_STRING "2021-06-04"
55

66
#endif // DEFINITIONS_H
77

0 commit comments

Comments
 (0)