summaryrefslogtreecommitdiffstats
path: root/kexi/widget/utils/kexicontextmenuutils.cpp
blob: f34864a989e5e681c62841d95d3ee02358efe241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* This file is part of the KDE project
   Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexicontextmenuutils.h"

#include <kactioncollection.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <kimageio.h>
#include <kdebug.h>
#include <kmessagebox.h>

#include <tqfiledialog.h>
#include <tqapplication.h>

#ifdef TQ_WS_WIN
#include <win32_utils.h>
#include <krecentdirs.h>
#endif

//! @internal
class KexiImageContextMenu::Private
{
public:
	Private(TQWidget *parent)
	 : actionCollection(parent)
	{
	}

	KActionCollection actionCollection;
	KAction *insertFromFileAction, *saveAsAction, *cutAction, *copyAction, *pasteAction,
		*deleteAction
#ifdef KEXI_NO_UNFINISHED 
		, *propertiesAction
#endif
		;
};

//------------

KexiImageContextMenu::KexiImageContextMenu(TQWidget* parent)
 : KPopupMenu(parent)
 , d( new Private(this) )
{
	setName("KexiImageContextMenu");
	insertTitle(TQString());

	d->insertFromFileAction = new KAction(i18n("Insert From &File..."), SmallIconSet("fileopen"), 0,
			TQT_TQOBJECT(this), TQT_SLOT(insertFromFile()), &d->actionCollection, "insert");
	d->insertFromFileAction->plug(this);
	d->saveAsAction = KStdAction::saveAs(TQT_TQOBJECT(this), TQT_SLOT(saveAs()), &d->actionCollection);
//	d->saveAsAction->setText(i18n("&Save &As..."));
	d->saveAsAction->plug(this);
	insertSeparator();
	d->cutAction = KStdAction::cut(TQT_TQOBJECT(this), TQT_SLOT(cut()), &d->actionCollection);
	d->cutAction->plug(this);
	d->copyAction = KStdAction::copy(TQT_TQOBJECT(this), TQT_SLOT(copy()), &d->actionCollection);
	d->copyAction->plug(this);
	d->pasteAction = KStdAction::paste(TQT_TQOBJECT(this), TQT_SLOT(paste()), &d->actionCollection);
	d->pasteAction->plug(this);
	d->deleteAction = new KAction(i18n("&Clear"), SmallIconSet("editdelete"), 0,
		TQT_TQOBJECT(this), TQT_SLOT(clear()), &d->actionCollection, "delete");
	d->deleteAction->plug(this);
#ifdef KEXI_NO_UNFINISHED 
	d->propertiesAction = 0;
#else
	insertSeparator();
	d->propertiesAction = new KAction(i18n("Properties"), 0, 0,
		this, TQT_SLOT(showProperties()), &d->actionCollection, "properties");
	d->propertiesAction->plug(this);
#endif
	connect(this, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(updateActionsAvailability()));
}

KexiImageContextMenu::~KexiImageContextMenu()
{
	delete d;
}

void KexiImageContextMenu::insertFromFile()
{
//	TQWidget *focusWidget = tqApp->focusWidget();
#ifdef TQ_WS_WIN
	TQString recentDir;
	TQString fileName = TQFileDialog::getOpenFileName(
		KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path(), 
		convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Reading)), 
		this, 0, i18n("Insert Image From File"));
	KURL url;
	if (!fileName.isEmpty())
		url.setPath( fileName );
#else
	KURL url( KFileDialog::getImageOpenURL(
		":LastVisitedImagePath", this, i18n("Insert Image From File")) );
//	TQString fileName = url.isLocalFile() ? url.path() : url.prettyURL();

	//! @todo download the file if remote, then set fileName properly
#endif
	if (!url.isValid()) {
		//focus the app again because to avoid annoying the user with unfocused main window
		if (tqApp->mainWidget()) {
			//focusWidget->raise();
			//focusWidget->setFocus();
			tqApp->mainWidget()->raise();
		}
		return;
	}
	kexipluginsdbg << "fname=" << url.prettyURL() << endl;

#ifdef TQ_WS_WIN
	//save last visited path
//	KURL url(fileName);
	if (url.isLocalFile())
		KRecentDirs::add(":LastVisitedImagePath", url.directory());
#endif

	emit insertFromFileRequested(url);
	if (tqApp->mainWidget()) {
//		focusWidget->raise();
//		focusWidget->setFocus();
		tqApp->mainWidget()->raise();
	}
}

void KexiImageContextMenu::saveAs()
{
	TQString origFilename, fileExtension;
	bool dataIsEmpty = false;
	emit aboutToSaveAsRequested(origFilename, fileExtension, dataIsEmpty);

	if (dataIsEmpty) {
		kdWarning() << "KexiImageContextMenu::saveAs(): no data!" << endl;
		return;
	}
	if (!origFilename.isEmpty())
		origFilename = TQString("/") + origFilename;

	if (fileExtension.isEmpty()) {
		// PNG data is the default
		fileExtension = "png";
	}
	
#ifdef TQ_WS_WIN
	TQString recentDir;
	TQString fileName = TQFileDialog::getSaveFileName(
		KFileDialog::getStartURL(":LastVisitedImagePath", recentDir).path() + origFilename,
		convertKFileDialogFilterToTQFileDialogFilter(KImageIO::pattern(KImageIO::Writing)), 
		this, 0, i18n("Save Image to File"));
#else
	//! @todo add originalFileName! (requires access to KRecentDirs)
	TQString fileName = KFileDialog::getSaveFileName(
		":LastVisitedImagePath", KImageIO::pattern(KImageIO::Writing), this, i18n("Save Image to File"));
#endif
	if (fileName.isEmpty())
		return;
	
	if (TQFileInfo(fileName).extension().isEmpty())
		fileName += (TQString(".")+fileExtension);
	kdDebug() << fileName << endl;
	KURL url;
	url.setPath( fileName );

#ifdef TQ_WS_WIN
	//save last visited path
	if (url.isLocalFile())
		KRecentDirs::add(":LastVisitedImagePath", url.directory());
#endif

	TQFile f(fileName);
	if (f.exists() && KMessageBox::Yes != KMessageBox::warningYesNo(this, 
		"<qt>"+i18n("File \"%1\" already exists."
		"<p>Do you want to replace it with a new one?")
		.tqarg(TQDir::convertSeparators(fileName))+"</qt>",0, 
		KGuiItem(i18n("&Replace")), KGuiItem(i18n("&Don't Replace"))))
	{
		return;
	}

//! @todo use KURL?
	emit saveAsRequested(fileName);
}

void KexiImageContextMenu::cut()
{
	emit cutRequested();
}

void KexiImageContextMenu::copy()
{
	emit copyRequested();
}

void KexiImageContextMenu::paste()
{
	emit pasteRequested();
}

void KexiImageContextMenu::clear()
{
	emit clearRequested();
}

void KexiImageContextMenu::showProperties()
{
	emit showPropertiesRequested();
}

void KexiImageContextMenu::updateActionsAvailability()
{
	bool valueIsNull = true;
	bool valueIsReadOnly = true;
	emit updateActionsAvailabilityRequested(valueIsNull, valueIsReadOnly);

	d->insertFromFileAction->setEnabled( !valueIsReadOnly );
	d->saveAsAction->setEnabled( !valueIsNull );
	d->cutAction->setEnabled( !valueIsNull && !valueIsReadOnly );
	d->copyAction->setEnabled( !valueIsNull );
	d->pasteAction->setEnabled( !valueIsReadOnly );
	d->deleteAction->setEnabled( !valueIsNull && !valueIsReadOnly );
	if (d->propertiesAction)
		d->propertiesAction->setEnabled( !valueIsNull );
}

KActionCollection* KexiImageContextMenu::actionCollection() const
{
	return &d->actionCollection;
}

//static
bool KexiImageContextMenu::updateTitle(TQPopupMenu *menu, const TQString& title, const TQString& iconName)
{
	return KexiContextMenuUtils::updateTitle(menu, title, i18n("Image"), iconName);
}

// -------------------------------------------

//static
bool KexiContextMenuUtils::updateTitle(TQPopupMenu *menu, const TQString& objectName, 
	const TQString& objectTypeName, const TQString& iconName)
{
	if (!menu || objectName.isEmpty() || objectTypeName.isEmpty())
		return false;
	const int id = menu->idAt(0);
	TQMenuItem *item = menu->findItem(id);
	if (!item)
		return false;
	KPopupTitle *title = dynamic_cast<KPopupTitle *>(item->widget());
	if (!title)
		return false;

/*! @todo look at makeFirstCharacterUpperCaseInCaptions setting [bool]
 (see doc/dev/settings.txt) */
	TQString realTitle( i18n("Object name : Object type", "%1 : %2")
		.tqarg( objectName[0].upper() + objectName.mid(1) )
		.tqarg( objectTypeName ));

	if (iconName.isEmpty())
		title->setTitle(realTitle);
	else {
		TQPixmap pixmap(SmallIcon( iconName ));
		title->setTitle(realTitle, &pixmap);
	}
	return true;
}

#include "kexicontextmenuutils.moc"