diff options
Diffstat (limited to 'ksnapshot/ksnapshot.cpp')
-rw-r--r-- | ksnapshot/ksnapshot.cpp | 195 |
1 files changed, 153 insertions, 42 deletions
diff --git a/ksnapshot/ksnapshot.cpp b/ksnapshot/ksnapshot.cpp index ad5df57c..0743de97 100644 --- a/ksnapshot/ksnapshot.cpp +++ b/ksnapshot/ksnapshot.cpp @@ -6,6 +6,7 @@ * (c) Aaron J. Seigo 2002 * (c) Nadeem Hasan 2003 * (c) Bernd Brandstetter 2004 + * (c) Emanoil Kotsev 2023 * * Released under the LGPL see file LICENSE for details. */ @@ -35,6 +36,9 @@ #include <tdepopupmenu.h> #include <kpushbutton.h> #include <tdestartupinfo.h> +#include <kiconloader.h> +#include <kprocess.h> +#include <krun.h> #include <tqcursor.h> #include <tqregexp.h> @@ -72,12 +76,13 @@ KSnapshot::KSnapshot(TQWidget *parent, const char *name, bool grabCurrent) TQVBox *vbox = makeVBoxMainWidget(); mainWidget = new KSnapshotWidget( vbox, "mainWidget" ); - connect(mainWidget, TQT_SIGNAL(startImageDrag()), TQT_SLOT(slotDragSnapshot())); - - connect( mainWidget, TQT_SIGNAL( newClicked() ), TQT_SLOT( slotGrab() ) ); - connect( mainWidget, TQT_SIGNAL( saveClicked() ), TQT_SLOT( slotSaveAs() ) ); - connect( mainWidget, TQT_SIGNAL( printClicked() ), TQT_SLOT( slotPrint() ) ); - connect( mainWidget, TQT_SIGNAL( copyClicked() ), TQT_SLOT( slotCopy() ) ); + connect(mainWidget, TQ_SIGNAL(startImageDrag()), TQ_SLOT(slotDragSnapshot())); + connect(mainWidget, TQ_SIGNAL(newClicked()), TQ_SLOT(slotGrab())); + connect(mainWidget, TQ_SIGNAL(saveClicked()), TQ_SLOT(slotSaveAs())); + connect(mainWidget, TQ_SIGNAL(printClicked()), TQ_SLOT(slotPrint())); + connect(mainWidget, TQ_SIGNAL(copyClicked()), TQ_SLOT(slotCopy())); + connect(mainWidget, TQ_SIGNAL(openWithKPClicked()), TQ_SLOT(slotOpenWithKP())); + connect(tqApp, TQ_SIGNAL(aboutToQuit()), TQ_SLOT(slotAboutToQuit())); grabber->show(); grabber->grabMouse( waitCursor ); @@ -106,39 +111,59 @@ KSnapshot::KSnapshot(TQWidget *parent, const char *name, bool grabCurrent) autoincFilename(); } - connect( &grabTimer, TQT_SIGNAL( timeout() ), TQT_TQOBJECT(this), TQT_SLOT( grabTimerDone() ) ); - connect( &updateTimer, TQT_SIGNAL( timeout() ), TQT_TQOBJECT(this), TQT_SLOT( updatePreview() ) ); - TQTimer::singleShot( 0, TQT_TQOBJECT(this), TQT_SLOT( updateCaption() ) ); + connect( &grabTimer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( grabTimerDone() ) ); + connect( &updateTimer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( updatePreview() ) ); + TQTimer::singleShot( 0, this, TQ_SLOT( updateCaption() ) ); KHelpMenu *helpMenu = new KHelpMenu(this, TDEGlobal::instance()->aboutData(), false); TQPushButton *helpButton = actionButton( Help ); helpButton->setPopup(helpMenu->menu()); + // Populate Open With... menu + TDEPopupMenu *popupOpenWith = new TDEPopupMenu(this); + openWithOffers = TDETrader::self()->query("image/png", "Type == 'Application'"); + int i = 0; + for (TDETrader::OfferList::Iterator it = openWithOffers.begin(); it != openWithOffers.end(); ++it) + { + popupOpenWith->insertItem(SmallIcon((*it)->icon()), (*it)->name(), i); + ++i; // we need menu ids to match with OfferList indexes + } + mainWidget->btnOpenWith->setPopup(popupOpenWith); + connect(popupOpenWith, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotOpenWith(int))); + + // Check for KolourPaint availability + KService::Ptr kpaint = KService::serviceByDesktopName("kolourpaint"); + if (!kpaint) { + mainWidget->btnOpenWithKP->hide(); + } + TDEAccel* accel = new TDEAccel(this); - accel->insert(TDEStdAccel::Quit, TQT_TQOBJECT(kapp), TQT_SLOT(quit())); + accel->insert(TDEStdAccel::Quit, kapp, TQ_SLOT(quit())); accel->insert( "QuickSave", i18n("Quick Save Snapshot &As..."), i18n("Save the snapshot to the file specified by the user without showing the file dialog."), - CTRL+SHIFT+Key_S, TQT_TQOBJECT(this), TQT_SLOT(slotSave())); - accel->insert(TDEStdAccel::Save, TQT_TQOBJECT(this), TQT_SLOT(slotSaveAs())); -// accel->insert(TDEShortcut(CTRL+Key_A), TQT_TQOBJECT(this), TQT_SLOT(slotSaveAs())); + CTRL+SHIFT+Key_S, this, TQ_SLOT(slotSave())); + accel->insert(TDEStdAccel::Save, this, TQ_SLOT(slotSaveAs())); +// accel->insert(TDEShortcut(CTRL+Key_A), this, TQ_SLOT(slotSaveAs())); accel->insert( "SaveAs", i18n("Save Snapshot &As..."), i18n("Save the snapshot to the file specified by the user."), - CTRL+Key_A, TQT_TQOBJECT(this), TQT_SLOT(slotSaveAs())); - accel->insert(TDEStdAccel::Print, TQT_TQOBJECT(this), TQT_SLOT(slotPrint())); - accel->insert(TDEStdAccel::New, TQT_TQOBJECT(this), TQT_SLOT(slotGrab())); - accel->insert(TDEStdAccel::Copy, TQT_TQOBJECT(this), TQT_SLOT(slotCopy())); + CTRL+Key_A, this, TQ_SLOT(slotSaveAs())); + accel->insert(TDEStdAccel::Print, this, TQ_SLOT(slotPrint())); + accel->insert(TDEStdAccel::New, this, TQ_SLOT(slotGrab())); + accel->insert(TDEStdAccel::Copy, this, TQ_SLOT(slotCopy())); - accel->insert( "Quit2", Key_Q, TQT_TQOBJECT(this), TQT_SLOT(slotSave())); - accel->insert( "Save2", Key_S, TQT_TQOBJECT(this), TQT_SLOT(slotSaveAs())); - accel->insert( "Print2", Key_P, TQT_TQOBJECT(this), TQT_SLOT(slotPrint())); - accel->insert( "New2", Key_N, TQT_TQOBJECT(this), TQT_SLOT(slotGrab())); - accel->insert( "New3", Key_Space, TQT_TQOBJECT(this), TQT_SLOT(slotGrab())); + accel->insert( "Quit2", Key_Q, this, TQ_SLOT(slotSave())); + accel->insert( "Save2", Key_S, this, TQ_SLOT(slotSaveAs())); + accel->insert( "Print2", Key_P, this, TQ_SLOT(slotPrint())); + accel->insert( "New2", Key_N, this, TQ_SLOT(slotGrab())); + accel->insert( "New3", Key_Space, this, TQ_SLOT(slotGrab())); setEscapeButton( User1 ); - connect( this, TQT_SIGNAL( user1Clicked() ), TQT_SLOT( reject() ) ); + connect( this, TQ_SIGNAL( user1Clicked() ), TQ_SLOT( reject() ) ); mainWidget->btnNew->setFocus(); + + oldWinPos = pos(); } KSnapshot::~KSnapshot() @@ -254,15 +279,16 @@ void KSnapshot::slotDragSnapshot() void KSnapshot::slotGrab() { + oldWinPos = pos(); hide(); - if ( mainWidget->delay() && mainWidget->mode() != Region ) + if ( mainWidget->delay() ) grabTimer.start( mainWidget->delay() * 1000, true ); else { if ( mainWidget->mode() == Region ) { rgnGrab = new RegionGrabber(); - connect( rgnGrab, TQT_SIGNAL( regionGrabbed( const TQPixmap & ) ), - TQT_SLOT( slotRegionGrabbed( const TQPixmap & ) ) ); + connect( rgnGrab, TQ_SIGNAL( regionGrabbed( const TQPixmap & ) ), + TQ_SLOT( slotRegionGrabbed( const TQPixmap & ) ) ); } else { grabber->show(); @@ -312,7 +338,7 @@ void KSnapshot::slotPrint() neww = newh/h*w; } - img = img.smoothScale( int(neww), int(newh), TQ_ScaleMin ); + img = img.smoothScale( int(neww), int(newh), TQImage::ScaleMin ); tqApp->processEvents(); int x = (metrics.width()-img.width())/2; @@ -342,6 +368,7 @@ void KSnapshot::slotRegionGrabbed( const TQPixmap &pix ) delete rgnGrab; TQApplication::restoreOverrideCursor(); + move(oldWinPos); show(); } @@ -356,29 +383,107 @@ void KSnapshot::slotWindowGrabbed( const TQPixmap &pix ) } TQApplication::restoreOverrideCursor(); + move(oldWinPos); show(); } +void KSnapshot::slotOpenWith(int id) +{ + openWithExternalApp(*openWithOffers[id]); +} + +void KSnapshot::slotOpenWithKP() { + KService::Ptr kpaint = KService::serviceByDesktopName("kolourpaint"); + if (kpaint) { + openWithExternalApp(*kpaint); + } +} + +void KSnapshot::openWithExternalApp(const KService &service) { + // Write snapshot to temporary file + bool ok = false; + KTempFile *tmpFile = new KTempFile; + if (tmpFile->status() == 0) { + if (snapshot.save(tmpFile->file(), "PNG")) { + if (tmpFile->close()) { + ok = true; + } + } + } + + if (!ok) { + KMessageBox::error(this, i18n("KSnapshot was unable to create temporary file."), + i18n("Unable to save image")); + delete tmpFile; + return; + } + + // Launch application + KURL::List list; + list.append(tmpFile->name()); + TQStringList args = KRun::processDesktopExec(service, list, false, false); + + TDEProcess *externalApp = new TDEProcess; + *externalApp << args; + connect(externalApp, TQ_SIGNAL(processExited(TDEProcess*)), + this, TQ_SLOT(slotExternalAppClosed(TDEProcess*))); + + if (!externalApp->start(TDEProcess::OwnGroup)) { + KMessageBox::error(this, i18n("Cannot start %1!").arg(service.name())); + delete tmpFile; + return; + } + + m_tmpFiles[externalApp] = tmpFile; +} + +void KSnapshot::slotExternalAppClosed(TDEProcess *process) +{ + if (process && m_tmpFiles.contains(process)) + { + KTempFile *tmpFile = m_tmpFiles[process]; + if (tmpFile) + { + snapshot.load(tmpFile->name()); + updatePreview(); + tmpFile->unlink(); + delete tmpFile; + } + m_tmpFiles.remove(process); + } +} + +void KSnapshot::slotAboutToQuit() +{ + for (KTempFile *tmpFile : m_tmpFiles) + { + tmpFile->unlink(); + delete tmpFile; + } + m_tmpFiles.clear(); + + TDEConfig *conf=TDEGlobal::config(); + conf->setGroup("GENERAL"); + conf->writeEntry("delay",mainWidget->delay()); + conf->writeEntry("mode",mainWidget->mode()); + conf->writeEntry("includeDecorations",mainWidget->includeDecorations()); + KURL url = filename; + url.setPass( TQString() ); + conf->writePathEntry("filename",url.url()); +} + void KSnapshot::closeEvent( TQCloseEvent * e ) { - TDEConfig *conf=TDEGlobal::config(); - conf->setGroup("GENERAL"); - conf->writeEntry("delay",mainWidget->delay()); - conf->writeEntry("mode",mainWidget->mode()); - conf->writeEntry("includeDecorations",mainWidget->includeDecorations()); - KURL url = filename; - url.setPass( TQString() ); - conf->writePathEntry("filename",url.url()); - e->accept(); + e->accept(); } bool KSnapshot::eventFilter( TQObject* o, TQEvent* e) { - if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(grabber) && e->type() == TQEvent::MouseButtonPress ) { + if ( o == grabber && e->type() == TQEvent::MouseButtonPress ) { TQMouseEvent* me = (TQMouseEvent*) e; if ( TQWidget::mouseGrabber() != grabber ) return false; - if ( me->button() == Qt::LeftButton ) + if ( me->button() == TQt::LeftButton ) performGrab(); } return false; @@ -430,8 +535,8 @@ void KSnapshot::grabTimerDone() { if ( mainWidget->mode() == Region ) { rgnGrab = new RegionGrabber(); - connect( rgnGrab, TQT_SIGNAL( regionGrabbed( const TQPixmap & ) ), - TQT_SLOT( slotRegionGrabbed( const TQPixmap & ) ) ); + connect( rgnGrab, TQ_SIGNAL( regionGrabbed( const TQPixmap & ) ), + TQ_SLOT( slotRegionGrabbed( const TQPixmap & ) ) ); } else { performGrab(); @@ -446,8 +551,8 @@ void KSnapshot::performGrab() grabTimer.stop(); if ( mainWidget->mode() == ChildWindow ) { WindowGrabber wndGrab; - connect( &wndGrab, TQT_SIGNAL( windowGrabbed( const TQPixmap & ) ), - TQT_SLOT( slotWindowGrabbed( const TQPixmap & ) ) ); + connect( &wndGrab, TQ_SIGNAL( windowGrabbed( const TQPixmap & ) ), + TQ_SLOT( slotWindowGrabbed( const TQPixmap & ) ) ); wndGrab.exec(); } else if ( mainWidget->mode() == WindowUnderCursor ) { @@ -460,6 +565,7 @@ void KSnapshot::performGrab() TQApplication::restoreOverrideCursor(); modified = true; updateCaption(); + move(oldWinPos); show(); } @@ -483,6 +589,11 @@ void KSnapshot::setURL( const TQString &url ) updateCaption(); } +void KSnapshot::setPixmap(const TQPixmap &newImage) { + snapshot = newImage; + updatePreview(); +} + void KSnapshot::setGrabMode( int m ) { mainWidget->setMode( m ); |