kexi

kexicontextmenuutils.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006-2007 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 "kexicontextmenuutils.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 unfocused main window
00118         if (qApp->mainWidget()) {
00119             //focusWidget->raise();
00120             //focusWidget->setFocus();
00121             qApp->mainWidget()->raise();
00122         }
00123         return;
00124     }
00125     kexipluginsdbg << "fname=" << url.prettyURL() << endl;
00126 
00127 #ifdef Q_WS_WIN
00128     //save last visited path
00129 //  KURL url(fileName);
00130     if (url.isLocalFile())
00131         KRecentDirs::add(":LastVisitedImagePath", url.directory());
00132 #endif
00133 
00134     emit insertFromFileRequested(url);
00135     if (qApp->mainWidget()) {
00136 //      focusWidget->raise();
00137 //      focusWidget->setFocus();
00138         qApp->mainWidget()->raise();
00139     }
00140 }
00141 
00142 void KexiImageContextMenu::saveAs()
00143 {
00144     QString origFilename, fileExtension;
00145     bool dataIsEmpty = false;
00146     emit aboutToSaveAsRequested(origFilename, fileExtension, dataIsEmpty);
00147 
00148     if (dataIsEmpty) {
00149         kdWarning() << "KexiImageContextMenu::saveAs(): no data!" << endl;
00150         return;
00151     }
00152     if (!origFilename.isEmpty())
00153         origFilename = QString("/") + origFilename;
00154 
00155     if (fileExtension.isEmpty()) {
00156         // PNG data is the default
00157         fileExtension = "png";
00158     }
00159     
00160 #ifdef Q_WS_WIN
00161     QString recentDir;
00162     QString fileName = QFileDialog::getSaveFileName(
00163         KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path() + origFilename,
00164         convertKFileDialogFilterToQFileDialogFilter(KImageIO::pattern(KImageIO::Writing)), 
00165         this, 0, i18n("Save Image to File"));
00166 #else
00168     QString fileName = KFileDialog::getSaveFileName(
00169         ":LastVisitedImagePath", KImageIO::pattern(KImageIO::Writing), this, i18n("Save Image to File"));
00170 #endif
00171     if (fileName.isEmpty())
00172         return;
00173     
00174     if (QFileInfo(fileName).extension().isEmpty())
00175         fileName += (QString(".")+fileExtension);
00176     kdDebug() << fileName << endl;
00177     KURL url;
00178     url.setPath( fileName );
00179 
00180 #ifdef Q_WS_WIN
00181     //save last visited path
00182     if (url.isLocalFile())
00183         KRecentDirs::add(":LastVisitedImagePath", url.directory());
00184 #endif
00185 
00186     QFile f(fileName);
00187     if (f.exists() && KMessageBox::Yes != KMessageBox::warningYesNo(this, 
00188         "<qt>"+i18n("File \"%1\" already exists."
00189         "<p>Do you want to replace it with a new one?")
00190         .arg(QDir::convertSeparators(fileName))+"</qt>",0, 
00191         KGuiItem(i18n("&Replace")), KGuiItem(i18n("&Don't Replace"))))
00192     {
00193         return;
00194     }
00195 
00197     emit saveAsRequested(fileName);
00198 }
00199 
00200 void KexiImageContextMenu::cut()
00201 {
00202     emit cutRequested();
00203 }
00204 
00205 void KexiImageContextMenu::copy()
00206 {
00207     emit copyRequested();
00208 }
00209 
00210 void KexiImageContextMenu::paste()
00211 {
00212     emit pasteRequested();
00213 }
00214 
00215 void KexiImageContextMenu::clear()
00216 {
00217     emit clearRequested();
00218 }
00219 
00220 void KexiImageContextMenu::showProperties()
00221 {
00222     emit showPropertiesRequested();
00223 }
00224 
00225 void KexiImageContextMenu::updateActionsAvailability()
00226 {
00227     bool valueIsNull = true;
00228     bool valueIsReadOnly = true;
00229     emit updateActionsAvailabilityRequested(valueIsNull, valueIsReadOnly);
00230 
00231     d->insertFromFileAction->setEnabled( !valueIsReadOnly );
00232     d->saveAsAction->setEnabled( !valueIsNull );
00233     d->cutAction->setEnabled( !valueIsNull && !valueIsReadOnly );
00234     d->copyAction->setEnabled( !valueIsNull );
00235     d->pasteAction->setEnabled( !valueIsReadOnly );
00236     d->deleteAction->setEnabled( !valueIsNull && !valueIsReadOnly );
00237     if (d->propertiesAction)
00238         d->propertiesAction->setEnabled( !valueIsNull );
00239 }
00240 
00241 KActionCollection* KexiImageContextMenu::actionCollection() const
00242 {
00243     return &d->actionCollection;
00244 }
00245 
00246 //static
00247 bool KexiImageContextMenu::updateTitle(QPopupMenu *menu, const QString& title, const QString& iconName)
00248 {
00249     return KexiContextMenuUtils::updateTitle(menu, title, i18n("Image"), iconName);
00250 }
00251 
00252 // -------------------------------------------
00253 
00254 //static
00255 bool KexiContextMenuUtils::updateTitle(QPopupMenu *menu, const QString& objectName, 
00256     const QString& objectTypeName, const QString& iconName)
00257 {
00258     if (!menu || objectName.isEmpty() || objectTypeName.isEmpty())
00259         return false;
00260     const int id = menu->idAt(0);
00261     QMenuItem *item = menu->findItem(id);
00262     if (!item)
00263         return false;
00264     KPopupTitle *title = dynamic_cast<KPopupTitle *>(item->widget());
00265     if (!title)
00266         return false;
00267 
00270     QString realTitle( i18n("Object name : Object type", "%1 : %2")
00271         .arg( objectName[0].upper() + objectName.mid(1) )
00272         .arg( objectTypeName ));
00273 
00274     if (iconName.isEmpty())
00275         title->setTitle(realTitle);
00276     else {
00277         QPixmap pixmap(SmallIcon( iconName ));
00278         title->setTitle(realTitle, &pixmap);
00279     }
00280     return true;
00281 }
00282 
00283 #include "kexicontextmenuutils.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys