kexi

kexiimagecontextmenu.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexiimagecontextmenu.h"
00021 
00022 #include <kactioncollection.h>
00023 #include <klocale.h>
00024 #include <kiconloader.h>
00025 #include <kfiledialog.h>
00026 #include <kimageio.h>
00027 #include <kdebug.h>
00028 #include <kmessagebox.h>
00029 
00030 #include <qfiledialog.h>
00031 #include <qapplication.h>
00032 
00033 #ifdef Q_WS_WIN
00034 #include <win32_utils.h>
00035 #include <krecentdirs.h>
00036 #endif
00037 
00039 class KexiImageContextMenu::Private
00040 {
00041 public:
00042     Private(QWidget *parent)
00043      : actionCollection(parent)
00044     {
00045     }
00046 
00047     KActionCollection actionCollection;
00048     KAction *insertFromFileAction, *saveAsAction, *cutAction, *copyAction, *pasteAction,
00049         *deleteAction
00050 #ifdef KEXI_NO_UNFINISHED 
00051         , *propertiesAction
00052 #endif
00053         ;
00054 };
00055 
00056 //------------
00057 
00058 KexiImageContextMenu::KexiImageContextMenu(QWidget* parent)
00059  : KPopupMenu(parent)
00060  , d( new Private(this) )
00061 {
00062     setName("KexiImageContextMenu");
00063     insertTitle(QString::null);
00064 
00065     d->insertFromFileAction = new KAction(i18n("Insert From &File..."), SmallIconSet("fileopen"), 0,
00066             this, SLOT(insertFromFile()), &d->actionCollection, "insert");
00067     d->insertFromFileAction->plug(this);
00068     d->saveAsAction = KStdAction::saveAs(this, SLOT(saveAs()), &d->actionCollection);
00069 //  d->saveAsAction->setText(i18n("&Save &As..."));
00070     d->saveAsAction->plug(this);
00071     insertSeparator();
00072     d->cutAction = KStdAction::cut(this, SLOT(cut()), &d->actionCollection);
00073     d->cutAction->plug(this);
00074     d->copyAction = KStdAction::copy(this, SLOT(copy()), &d->actionCollection);
00075     d->copyAction->plug(this);
00076     d->pasteAction = KStdAction::paste(this, SLOT(paste()), &d->actionCollection);
00077     d->pasteAction->plug(this);
00078     d->deleteAction = new KAction(i18n("&Clear"), SmallIconSet("editdelete"), 0,
00079         this, SLOT(clear()), &d->actionCollection, "delete");
00080     d->deleteAction->plug(this);
00081 #ifdef KEXI_NO_UNFINISHED 
00082     d->propertiesAction = 0;
00083 #else
00084     insertSeparator();
00085     d->propertiesAction = new KAction(i18n("Properties"), 0, 0,
00086         this, SLOT(showProperties()), &d->actionCollection, "properties");
00087     d->propertiesAction->plug(this);
00088 #endif
00089     connect(this, SIGNAL(aboutToShow()), this, SLOT(updateActionsAvailability()));
00090 }
00091 
00092 KexiImageContextMenu::~KexiImageContextMenu()
00093 {
00094     delete d;
00095 }
00096 
00097 void KexiImageContextMenu::insertFromFile()
00098 {
00099     QWidget *focusWidget = qApp->focusWidget();
00100 #ifdef Q_WS_WIN
00101     QString recentDir;
00102     QString fileName = QFileDialog::getOpenFileName(
00103         KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path(), 
00104         convertKFileDialogFilterToQFileDialogFilter(KImageIO::pattern(KImageIO::Reading)), 
00105         this, 0, i18n("Insert Image From File"));
00106     KURL url;
00107     if (!fileName.isEmpty())
00108         url.setPath( fileName );
00109 #else
00110     KURL url( KFileDialog::getImageOpenURL(
00111         ":LastVisitedImagePath", this, i18n("Insert Image From File")) );
00112 //  QString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
00113 
00115 #endif
00116     if (!url.isValid()) {
00117         //focus the app again because to avoid annoying the user with unfocesed main window
00118         if (focusWidget) {
00119             focusWidget->raise();
00120             focusWidget->setFocus();
00121         }
00122         return;
00123     }
00124     kexipluginsdbg << "fname=" << url.prettyURL() << endl;
00125 
00126 #ifdef Q_WS_WIN
00127     //save last visited path
00128 //  KURL url(fileName);
00129     if (url.isLocalFile())
00130         KRecentDirs::add(":LastVisitedImagePath", url.directory());
00131 #endif
00132 
00133     emit insertFromFileRequested(url);
00134     if (focusWidget) {
00135         focusWidget->raise();
00136         focusWidget->setFocus();
00137 // todo: fix
00138     }
00139 }
00140 
00141 void KexiImageContextMenu::saveAs()
00142 {
00143     QString origFilename, fileExtension;
00144     bool dataIsEmpty = false;
00145     emit aboutToSaveAsRequested(origFilename, fileExtension, dataIsEmpty);
00146 
00147     if (dataIsEmpty) {
00148         kdWarning() << "KexiImageContextMenu::saveAs(): no data!" << endl;
00149         return;
00150     }
00151     if (!origFilename.isEmpty())
00152         origFilename = QString("/") + origFilename;
00153 
00154     if (fileExtension.isEmpty()) {
00155         // PNG data is the default
00156         fileExtension = "png";
00157     }
00158     
00159 #ifdef Q_WS_WIN
00160     QString recentDir;
00161     QString fileName = QFileDialog::getSaveFileName(
00162         KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path() + origFilename,
00163         convertKFileDialogFilterToQFileDialogFilter(KImageIO::pattern(KImageIO::Writing)), 
00164         this, 0, i18n("Save Image to File"));
00165 #else
00167     QString fileName = KFileDialog::getSaveFileName(
00168         ":LastVisitedImagePath", KImageIO::pattern(KImageIO::Writing), this, i18n("Save Image to File"));
00169 #endif
00170     if (fileName.isEmpty())
00171         return;
00172     
00173     if (QFileInfo(fileName).extension().isEmpty())
00174         fileName += (QString(".")+fileExtension);
00175     kdDebug() << fileName << endl;
00176     KURL url;
00177     url.setPath( fileName );
00178 
00179 #ifdef Q_WS_WIN
00180     //save last visited path
00181     if (url.isLocalFile())
00182         KRecentDirs::add(":LastVisitedImagePath", url.directory());
00183 #endif
00184 
00185     QFile f(fileName);
00186     if (f.exists() && KMessageBox::Yes != KMessageBox::warningYesNo(this, 
00187         "<qt>"+i18n("File \"%1\" already exists."
00188         "<p>Do you want to replace it with a new one?")
00189         .arg(QDir::convertSeparators(fileName))+"</qt>",0, 
00190         KGuiItem(i18n("&Replace")), KGuiItem(i18n("&Don't Replace"))))
00191     {
00192         return;
00193     }
00194 
00196     emit saveAsRequested(fileName);
00197 }
00198 
00199 void KexiImageContextMenu::cut()
00200 {
00201     emit cutRequested();
00202 }
00203 
00204 void KexiImageContextMenu::copy()
00205 {
00206     emit copyRequested();
00207 }
00208 
00209 void KexiImageContextMenu::paste()
00210 {
00211     emit pasteRequested();
00212 }
00213 
00214 void KexiImageContextMenu::clear()
00215 {
00216     emit clearRequested();
00217 }
00218 
00219 void KexiImageContextMenu::showProperties()
00220 {
00221     emit showPropertiesRequested();
00222 }
00223 
00224 void KexiImageContextMenu::updateActionsAvailability()
00225 {
00226     bool valueIsNull = true;
00227     bool valueIsReadOnly = true;
00228     emit updateActionsAvailabilityRequested(valueIsNull, valueIsReadOnly);
00229 
00230     d->insertFromFileAction->setEnabled( !valueIsReadOnly );
00231     d->saveAsAction->setEnabled( !valueIsNull );
00232     d->cutAction->setEnabled( !valueIsNull && !valueIsReadOnly );
00233     d->copyAction->setEnabled( !valueIsNull );
00234     d->pasteAction->setEnabled( !valueIsReadOnly );
00235     d->deleteAction->setEnabled( !valueIsNull && !valueIsReadOnly );
00236     if (d->propertiesAction)
00237         d->propertiesAction->setEnabled( !valueIsNull );
00238 }
00239 
00240 KActionCollection* KexiImageContextMenu::actionCollection() const
00241 {
00242     return &d->actionCollection;
00243 }
00244 
00245 //static
00246 bool KexiImageContextMenu::updateTitle(QPopupMenu *menu, const QString& title, const QString& icon)
00247 {
00248     if (title.isEmpty())
00249         return false;
00250 
00253     QString realTitle = i18n("%1 : Image").arg( title[0].upper() + title.mid(1) );
00254 
00255     const int id = menu->idAt(0);
00256     QMenuItem *item = menu->findItem(id);
00257     if (item && dynamic_cast<KPopupTitle *>(item->widget())) {
00258         if (icon.isEmpty())
00259             dynamic_cast<KPopupTitle *>(item->widget())->setTitle(realTitle);
00260         else {
00261             QPixmap pixmap(SmallIcon( icon ));
00262             dynamic_cast<KPopupTitle *>(item->widget())->setTitle(realTitle, &pixmap);
00263         }
00264     }
00265     else
00266         return false;
00267 
00268     return true;
00269 }
00270 
00271 #include "kexiimagecontextmenu.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys