@@ -1832,6 +1832,9 @@ void ImageWindow::copyScreenshotToClipboard()
18321832}
18331833
18341834#include < QDateTime>
1835+ #include < QMimeData>
1836+ #include < QtNetwork>
1837+ #include < QTime>
18351838bool ImageWindow::pasteFromClipboard ()
18361839{
18371840 QClipboard *clipboard = QApplication::clipboard ();
@@ -1840,19 +1843,69 @@ bool ImageWindow::pasteFromClipboard()
18401843 cerr << " Clibpard is null!" << endl;
18411844 return false ;
18421845 }
1843- QImage cbImage = clipboard->image ();
1844- if (cbImage.isNull ())
1845- {
1846- cerr << " Clibpard does not contain an image!" << endl;
1846+ QMimeData const *data = clipboard->mimeData ();
1847+
1848+ cout << " Clipboard data formats: " << data->formats ().join (" , " ).toStdString () << endl;
1849+ if (data->hasImage ()) {
1850+ cout << " Clipboard has an image." << endl;
1851+ QImage cbImage = clipboard->image ();
1852+ if (cbImage.isNull ())
1853+ {
1854+ cerr << " Clibpard image could not be retrieved!" << endl;
1855+ return false ;
1856+ }
1857+ QString filename = QDir::tempPath () + " /temp_" + QString::number (QDateTime::currentMSecsSinceEpoch ()) + " .png" ;
1858+ if (cbImage.save (filename))
1859+ {
1860+ cout << " Saved clipboard image to " << filename.toStdString () << endl;
1861+ return this ->readImage (filename);
1862+ }
1863+ cerr << " Failed to save clipboard image to " << filename.toStdString () << endl;
18471864 return false ;
18481865 }
1849- QString filename = QDir::tempPath () + " /temp_" + QString::number (QDateTime::currentMSecsSinceEpoch ()) + " .png" ;
1850- if (cbImage.save (filename))
1851- {
1852- cout << " Saved clipboard image to " << filename.toStdString () << endl;
1853- return this ->readImage (filename);
1866+ if (data->hasText ()) {
1867+ cout << " Clipboard has text: " << data->text ().toStdString () << endl;
1868+ auto url = QUrl (data->text ());
1869+ bool downloadComplete = false ;
1870+ bool downloadFailed = false ;
1871+
1872+ QNetworkAccessManager *manager = new QNetworkAccessManager;
1873+ QNetworkRequest request (url);
1874+ QObject::connect (manager, &QNetworkAccessManager::finished, [&](QNetworkReply *reply) {
1875+ QByteArray ba= reply->readAll ();
1876+ QImage cbImage;
1877+ if (!cbImage.loadFromData (ba)) {
1878+ cerr << " Failed to download image from " << url.toString ().toStdString () << endl;
1879+ downloadFailed = true ;
1880+ }
1881+ 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))
1884+ {
1885+ cout << " Saved clipboard image to " << filename.toStdString () << endl;
1886+ this ->readImage (filename);
1887+ downloadComplete = true ;
1888+ } else {
1889+ cerr << " Failed to save clipboard image to " << filename.toStdString () << endl;
1890+ downloadFailed = true ;
1891+ }
1892+ });
1893+ manager->get (request);
1894+ QTime dieTime= QTime::currentTime ().addSecs (300 );
1895+
1896+ while (!downloadComplete && !downloadFailed && QTime::currentTime () < dieTime){
1897+ QCoreApplication::processEvents (QEventLoop::AllEvents, 100 );
1898+ }
1899+ if (!downloadComplete && !downloadFailed) {
1900+ cerr << " Timeout; download took too long." << endl;
1901+ return false ;
1902+ }
1903+ if (downloadFailed) {
1904+ return false ;
1905+ }
1906+ return true ;
18541907 }
1855- cerr << " Failed to save clipboard image to " << filename. toStdString () << endl;
1908+ cerr << " Unrecognized clipboard format. " << endl;
18561909 return false ;
18571910}
18581911
0 commit comments