kexicontextmenuutils.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexicontextmenuutils.h"
21
22#include <tdeactioncollection.h>
23#include <tdelocale.h>
24#include <kiconloader.h>
25#include <tdefiledialog.h>
26#include <kimageio.h>
27#include <kdebug.h>
28#include <tdemessagebox.h>
29
30#include <tqfiledialog.h>
31#include <tqapplication.h>
32
33#ifdef TQ_WS_WIN
34#include <win32_utils.h>
35#include <tderecentdirs.h>
36#endif
37
39class KexiImageContextMenu::Private
40{
41public:
42 Private(TQWidget *parent)
43 : actionCollection(parent)
44 {
45 }
46
47 TDEActionCollection actionCollection;
48 TDEAction *insertFromFileAction, *saveAsAction, *cutAction, *copyAction, *pasteAction,
49 *deleteAction
50#ifdef KEXI_NO_UNFINISHED
51 , *propertiesAction
52#endif
53 ;
54};
55
56//------------
57
58KexiImageContextMenu::KexiImageContextMenu(TQWidget* parent)
59 : TDEPopupMenu(parent)
60 , d( new Private(this) )
61{
62 setName("KexiImageContextMenu");
63 insertTitle(TQString());
64
65 d->insertFromFileAction = new TDEAction(i18n("Insert From &File..."), SmallIconSet("document-open"), 0,
66 this, TQ_SLOT(insertFromFile()), &d->actionCollection, "insert");
67 d->insertFromFileAction->plug(this);
68 d->saveAsAction = KStdAction::saveAs(this, TQ_SLOT(saveAs()), &d->actionCollection);
69// d->saveAsAction->setText(i18n("&Save &As..."));
70 d->saveAsAction->plug(this);
71 insertSeparator();
72 d->cutAction = KStdAction::cut(this, TQ_SLOT(cut()), &d->actionCollection);
73 d->cutAction->plug(this);
74 d->copyAction = KStdAction::copy(this, TQ_SLOT(copy()), &d->actionCollection);
75 d->copyAction->plug(this);
76 d->pasteAction = KStdAction::paste(this, TQ_SLOT(paste()), &d->actionCollection);
77 d->pasteAction->plug(this);
78 d->deleteAction = new TDEAction(i18n("&Clear"), SmallIconSet("edit-delete"), 0,
79 this, TQ_SLOT(clear()), &d->actionCollection, "delete");
80 d->deleteAction->plug(this);
81#ifdef KEXI_NO_UNFINISHED
82 d->propertiesAction = 0;
83#else
84 insertSeparator();
85 d->propertiesAction = new TDEAction(i18n("Properties"), 0, 0,
86 this, TQ_SLOT(showProperties()), &d->actionCollection, "properties");
87 d->propertiesAction->plug(this);
88#endif
89 connect(this, TQ_SIGNAL(aboutToShow()), this, TQ_SLOT(updateActionsAvailability()));
90}
91
92KexiImageContextMenu::~KexiImageContextMenu()
93{
94 delete d;
95}
96
98{
99// TQWidget *focusWidget = tqApp->focusWidget();
100#ifdef TQ_WS_WIN
101 TQString recentDir;
102 TQString fileName = TQFileDialog::getOpenFileName(
103 KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path(),
104 convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Reading)),
105 this, 0, i18n("Insert Image From File"));
106 KURL url;
107 if (!fileName.isEmpty())
108 url.setPath( fileName );
109#else
110 KURL url( KFileDialog::getImageOpenURL(
111 ":LastVisitedImagePath", this, i18n("Insert Image From File")) );
112// TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();
113
115#endif
116 if (!url.isValid()) {
117 //focus the app again because to avoid annoying the user with unfocused main window
118 if (tqApp->mainWidget()) {
119 //focusWidget->raise();
120 //focusWidget->setFocus();
121 tqApp->mainWidget()->raise();
122 }
123 return;
124 }
125 kexipluginsdbg << "fname=" << url.prettyURL() << endl;
126
127#ifdef TQ_WS_WIN
128 //save last visited path
129// KURL url(fileName);
130 if (url.isLocalFile())
131 TDERecentDirs::add(":LastVisitedImagePath", url.directory());
132#endif
133
134 emit insertFromFileRequested(url);
135 if (tqApp->mainWidget()) {
136// focusWidget->raise();
137// focusWidget->setFocus();
138 tqApp->mainWidget()->raise();
139 }
140}
141
143{
144 TQString origFilename, fileExtension;
145 bool dataIsEmpty = false;
146 emit aboutToSaveAsRequested(origFilename, fileExtension, dataIsEmpty);
147
148 if (dataIsEmpty) {
149 kdWarning() << "KexiImageContextMenu::saveAs(): no data!" << endl;
150 return;
151 }
152 if (!origFilename.isEmpty())
153 origFilename = TQString("/") + origFilename;
154
155 if (fileExtension.isEmpty()) {
156 // PNG data is the default
157 fileExtension = "png";
158 }
159
160#ifdef TQ_WS_WIN
161 TQString recentDir;
162 TQString fileName = TQFileDialog::getSaveFileName(
163 KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path() + origFilename,
164 convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Writing)),
165 this, 0, i18n("Save Image to File"));
166#else
168 TQString fileName = KFileDialog::getSaveFileName(
169 ":LastVisitedImagePath", KImageIO::pattern(KImageIO::Writing), this, i18n("Save Image to File"));
170#endif
171 if (fileName.isEmpty())
172 return;
173
174 if (TQFileInfo(fileName).extension().isEmpty())
175 fileName += (TQString(".")+fileExtension);
176 kdDebug() << fileName << endl;
177 KURL url;
178 url.setPath( fileName );
179
180#ifdef TQ_WS_WIN
181 //save last visited path
182 if (url.isLocalFile())
183 TDERecentDirs::add(":LastVisitedImagePath", url.directory());
184#endif
185
186 TQFile f(fileName);
187 if (f.exists() && KMessageBox::Yes != KMessageBox::warningYesNo(this,
188 "<qt>"+i18n("File \"%1\" already exists."
189 "<p>Do you want to replace it with a new one?")
190 .arg(TQDir::convertSeparators(fileName))+"</qt>",0,
191 KGuiItem(i18n("&Replace")), KGuiItem(i18n("&Don't Replace"))))
192 {
193 return;
194 }
195
197 emit saveAsRequested(fileName);
198}
199
200void KexiImageContextMenu::cut()
201{
202 emit cutRequested();
203}
204
205void KexiImageContextMenu::copy()
206{
207 emit copyRequested();
208}
209
210void KexiImageContextMenu::paste()
211{
212 emit pasteRequested();
213}
214
215void KexiImageContextMenu::clear()
216{
217 emit clearRequested();
218}
219
220void KexiImageContextMenu::showProperties()
221{
223}
224
225void KexiImageContextMenu::updateActionsAvailability()
226{
227 bool valueIsNull = true;
228 bool valueIsReadOnly = true;
229 emit updateActionsAvailabilityRequested(valueIsNull, valueIsReadOnly);
230
231 d->insertFromFileAction->setEnabled( !valueIsReadOnly );
232 d->saveAsAction->setEnabled( !valueIsNull );
233 d->cutAction->setEnabled( !valueIsNull && !valueIsReadOnly );
234 d->copyAction->setEnabled( !valueIsNull );
235 d->pasteAction->setEnabled( !valueIsReadOnly );
236 d->deleteAction->setEnabled( !valueIsNull && !valueIsReadOnly );
237 if (d->propertiesAction)
238 d->propertiesAction->setEnabled( !valueIsNull );
239}
240
241TDEActionCollection* KexiImageContextMenu::actionCollection() const
242{
243 return &d->actionCollection;
244}
245
246//static
247bool KexiImageContextMenu::updateTitle(TQPopupMenu *menu, const TQString& title, const TQString& iconName)
248{
249 return KexiContextMenuUtils::updateTitle(menu, title, i18n("Image"), iconName);
250}
251
252// -------------------------------------------
253
254//static
255bool KexiContextMenuUtils::updateTitle(TQPopupMenu *menu, const TQString& objectName,
256 const TQString& objectTypeName, const TQString& iconName)
257{
258 if (!menu || objectName.isEmpty() || objectTypeName.isEmpty())
259 return false;
260 const int id = menu->idAt(0);
261 TQMenuItem *item = menu->findItem(id);
262 if (!item)
263 return false;
264 TDEPopupTitle *title = dynamic_cast<TDEPopupTitle *>(item->widget());
265 if (!title)
266 return false;
267
270 TQString realTitle( i18n("Object name : Object type", "%1 : %2")
271 .arg( objectName[0].upper() + objectName.mid(1) )
272 .arg( objectTypeName ));
273
274 if (iconName.isEmpty())
275 title->setTitle(realTitle);
276 else {
277 TQPixmap pixmap(SmallIcon( iconName ));
278 title->setTitle(realTitle, &pixmap);
279 }
280 return true;
281}
282
283#include "kexicontextmenuutils.moc"
static bool updateTitle(TQPopupMenu *menu, const TQString &objectName, const TQString &objectTypeName, const TQString &iconName)
void copyRequested()
Emitted when "copy" action was requested.
void showPropertiesRequested()
Emitted when "showProperties" action was requested.
void cutRequested()
Emitted when "cut" action was requested.
void saveAsRequested(const TQString &fileName)
Emitted when "saveAs" action was requested.
void pasteRequested()
Emitted when "paste" action was requested.
static bool updateTitle(TQPopupMenu *menu, const TQString &title, const TQString &iconName=TQString())
void aboutToSaveAsRequested(TQString &origFilename, TQString &fileExtension, bool &dataIsEmpty)
void updateActionsAvailabilityRequested(bool &valueIsNull, bool &valueIsReadOnly)
Emitted when actions availability should be performed.
void insertFromFileRequested(const KURL &url)
void clearRequested()
Emitted when "clear" action was requested.