not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kmenuedit / basictab.cpp
blob4612c2ac6ffb93695ef1bf4df21c71ab465cdaf7
1 /*
2 * Copyright (C) 2000 Matthias Elter <elter@kde.org>
3 * Copyright (C) 2008 Laurent Montel <montel@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <QCheckBox>
23 #include <QLabel>
24 #include <QLayout>
25 #include <QFileInfo>
26 #include <QGroupBox>
28 //Added by qt3to4:
29 #include <QVBoxLayout>
30 #include <QGridLayout>
32 #include <klocale.h>
33 #include <kstandarddirs.h>
34 #include <kglobal.h>
35 #include <kdialog.h>
36 #include <kkeysequencewidget.h>
37 #include <klineedit.h>
38 #include <klinespellchecking.h>
39 #include <kmessagebox.h>
40 #include <kicondialog.h>
41 #include <kdesktopfile.h>
42 #include <kurlrequester.h>
43 #include <kfiledialog.h>
44 #include <kcombobox.h>
45 #include <kshell.h>
46 #include <khbox.h>
48 #ifndef Q_WS_WIN
49 #include "khotkeys.h"
50 #endif
52 #include "menuinfo.h"
54 #include "basictab.h"
55 #include "basictab.moc"
57 BasicTab::BasicTab( QWidget *parent )
58 : KTabWidget(parent)
60 _menuFolderInfo = 0;
61 _menuEntryInfo = 0;
63 // general group
64 QWidget *general_group = new QWidget();
65 QGridLayout *grid = new QGridLayout(general_group);
66 grid->setMargin( KDialog::marginHint() );
67 grid->setSpacing( KDialog::spacingHint() );
69 general_group->setAcceptDrops(false);
71 // setup line inputs
72 _nameEdit = new KLineEdit(general_group);
73 _nameEdit->setAcceptDrops(false);
74 _descriptionEdit = new KLineSpellChecking(general_group);
75 _descriptionEdit->setAcceptDrops(false);
76 _commentEdit = new KLineSpellChecking(general_group);
77 _commentEdit->setAcceptDrops(false);
78 _execEdit = new KUrlRequester(general_group);
79 _execEdit->lineEdit()->setAcceptDrops(false);
80 _execEdit->setWhatsThis(i18n(
81 "Following the command, you can have several place holders which will be replaced "
82 "with the actual values when the actual program is run:\n"
83 "%f - a single file name\n"
84 "%F - a list of files; use for applications that can open several local files at once\n"
85 "%u - a single URL\n"
86 "%U - a list of URLs\n"
87 "%d - the folder of the file to open\n"
88 "%D - a list of folders\n"
89 "%i - the icon\n"
90 "%m - the mini-icon\n"
91 "%c - the caption"));
93 _launchCB = new QCheckBox(i18n("Enable &launch feedback"), general_group);
94 _systrayCB = new QCheckBox(i18n("&Place in system tray"), general_group);
95 _onlyShowInKdeCB = new QCheckBox( i18n( "Only show in KDE" ), general_group );
96 _hiddenEntryCB = new QCheckBox( i18n( "Hidden entry" ), general_group );
97 _hiddenEntryCB->hide();
99 // setup labels
100 _nameLabel = new QLabel(i18n("&Name:"),general_group);
101 _nameLabel->setBuddy(_nameEdit);
102 _descriptionLabel = new QLabel(i18n("&Description:"),general_group);
103 _descriptionLabel->setBuddy(_descriptionEdit);
104 _commentLabel = new QLabel(i18n("&Comment:"),general_group);
105 _commentLabel->setBuddy(_commentEdit);
106 _execLabel = new QLabel(i18n("Co&mmand:"),general_group);
107 _execLabel->setBuddy(_execEdit);
108 grid->addWidget(_nameLabel, 0, 0);
109 grid->addWidget(_descriptionLabel, 1, 0);
110 grid->addWidget(_commentLabel, 2, 0);
111 grid->addWidget(_execLabel, 3, 0);
113 // connect line inputs
114 connect(_nameEdit, SIGNAL(textChanged(const QString&)),
115 SLOT(slotChanged()));
116 connect(_descriptionEdit, SIGNAL(textChanged(const QString&)),
117 SLOT(slotChanged()));
118 connect(_commentEdit, SIGNAL(textChanged(const QString&)),
119 SLOT(slotChanged()));
120 connect(_execEdit, SIGNAL(textChanged(const QString&)),
121 SLOT(slotChanged()));
122 connect(_execEdit, SIGNAL(urlSelected(const KUrl&)),
123 SLOT(slotExecSelected()));
124 connect(_launchCB, SIGNAL(clicked()), SLOT(launchcb_clicked()));
125 connect(_systrayCB, SIGNAL(clicked()), SLOT(systraycb_clicked()));
126 connect(_onlyShowInKdeCB, SIGNAL( clicked() ), SLOT( onlyshowcb_clicked() ) );
127 connect( _hiddenEntryCB, SIGNAL( clicked() ), SLOT( hiddenentrycb_clicked() ) );
128 // add line inputs to the grid
129 grid->addWidget(_nameEdit, 0, 1, 1, 1);
130 grid->addWidget(_descriptionEdit, 1, 1, 1, 1);
131 grid->addWidget(_commentEdit, 2, 1, 1, 2);
132 grid->addWidget(_execEdit, 3, 1, 1, 2);
133 grid->addWidget(_launchCB, 4, 0, 1, 3 );
134 grid->addWidget(_systrayCB, 5, 0, 1, 3 );
135 grid->addWidget(_onlyShowInKdeCB, 6, 0, 1, 3 );
136 grid->addWidget(_hiddenEntryCB, 7, 0, 1, 3 );
138 // setup icon button
139 _iconButton = new KIconButton(general_group);
140 _iconButton->setFixedSize(56,56);
141 _iconButton->setIconSize(48);
142 connect(_iconButton, SIGNAL(iconChanged(QString)), SLOT(slotChanged()));
143 grid->addWidget(_iconButton, 0, 2, 2, 1);
144 grid->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 8, 0, 1, 3);
146 // add the general group to the main layout
147 addTab(general_group, i18n("General"));
149 QWidget *advanced = new QWidget();
150 QVBoxLayout *advancedLayout = new QVBoxLayout(advanced);
152 // path group
153 _path_group = new QGroupBox(this);
154 QHBoxLayout *hboxLayout1 = new QHBoxLayout(_path_group);
155 hboxLayout1->setSpacing(KDialog::spacingHint());
156 hboxLayout1->setMargin(KDialog::marginHint());
158 _pathLabel = new QLabel(i18n("&Work path:"), _path_group);
159 hboxLayout1->addWidget(_pathLabel);
160 _pathEdit = new KUrlRequester(_path_group);
161 hboxLayout1->addWidget(_pathEdit);
162 _pathEdit->setMode(KFile::Directory | KFile::LocalOnly);
163 _pathEdit->lineEdit()->setAcceptDrops(false);
165 _pathLabel->setBuddy(_pathEdit);
167 connect(_pathEdit, SIGNAL(textChanged(const QString&)),
168 SLOT(slotChanged()));
169 advancedLayout->addWidget(_path_group);
171 // terminal group
172 _term_group = new QGroupBox(this);
173 QVBoxLayout *vbox = new QVBoxLayout(_term_group);
174 vbox->setMargin(KDialog::marginHint());
175 vbox->setSpacing(KDialog::spacingHint());
177 _terminalCB = new QCheckBox(i18n("Run in term&inal"), _term_group);
178 connect(_terminalCB, SIGNAL(clicked()), SLOT(termcb_clicked()));
179 vbox->addWidget(_terminalCB);
181 QWidget *hbox = new QWidget(_term_group);
182 QHBoxLayout *hboxLayout2 = new QHBoxLayout(hbox);
183 hbox->setLayout(hboxLayout2);
184 hboxLayout2->setSpacing(KDialog::spacingHint());
185 _termOptLabel = new QLabel(i18n("Terminal &options:"), hbox);
186 hboxLayout2->addWidget(_termOptLabel);
187 _termOptEdit = new KLineEdit(hbox);
188 hboxLayout2->addWidget(_termOptEdit);
189 _termOptEdit->setAcceptDrops(false);
190 _termOptLabel->setBuddy(_termOptEdit);
192 connect(_termOptEdit, SIGNAL(textChanged(const QString&)),
193 SLOT(slotChanged()));
194 vbox->addWidget(hbox);
195 advancedLayout->addWidget(_term_group);
197 _termOptEdit->setEnabled(false);
199 // uid group
200 _uid_group = new QGroupBox(this);
201 vbox = new QVBoxLayout(_uid_group);
202 vbox->setMargin(KDialog::marginHint());
203 vbox->setSpacing(KDialog::spacingHint());
205 _uidCB = new QCheckBox(i18n("&Run as a different user"), _uid_group);
206 connect(_uidCB, SIGNAL(clicked()), SLOT(uidcb_clicked()));
207 vbox->addWidget(_uidCB);
209 hbox = new QWidget(_uid_group);
210 QHBoxLayout *hboxLayout3 = new QHBoxLayout(hbox);
211 hbox->setLayout(hboxLayout3);
212 hboxLayout3->setSpacing(KDialog::spacingHint());
213 _uidLabel = new QLabel(i18n("&Username:"), hbox);
214 hboxLayout3->addWidget(_uidLabel);
215 _uidEdit = new KLineEdit(hbox);
216 hboxLayout3->addWidget(_uidEdit);
217 _uidEdit->setAcceptDrops(false);
218 _uidLabel->setBuddy(_uidEdit);
220 connect(_uidEdit, SIGNAL(textChanged(const QString&)),
221 SLOT(slotChanged()));
222 vbox->addWidget(hbox);
223 advancedLayout->addWidget(_uid_group);
225 _uidEdit->setEnabled(false);
227 // key binding group
228 general_group_keybind = new QGroupBox(this);
229 QHBoxLayout *keybindLayout = new QHBoxLayout(general_group_keybind);
230 keybindLayout->setMargin( KDialog::marginHint() );
231 keybindLayout->setSpacing( KDialog::spacingHint());
233 _keyEdit = new KKeySequenceWidget(general_group_keybind);
234 _keyEdit->setMultiKeyShortcutsAllowed(false);
235 QLabel *l = new QLabel( i18n("Current shortcut &key:"), general_group_keybind);
236 l->setBuddy( _keyEdit );
237 keybindLayout->addWidget(l);
238 connect( _keyEdit, SIGNAL(keySequenceChanged(const QKeySequence&)),
239 this, SLOT(slotCapturedKeySequence(const QKeySequence&)));
240 keybindLayout->addWidget(_keyEdit);
241 advancedLayout->addWidget( general_group_keybind );
243 advancedLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
245 addTab(advanced, i18n("Advanced"));
246 #ifndef Q_WS_WIN
247 if (!KHotKeys::present())
248 general_group_keybind->hide();
249 #endif
250 slotDisableAction();
253 void BasicTab::slotDisableAction()
255 //disable all group at the beginning.
256 //because there is not file selected.
257 _nameEdit->setEnabled(false);
258 _descriptionEdit->setEnabled(false);
259 _commentEdit->setEnabled(false);
260 _execEdit->setEnabled(false);
261 _launchCB->setEnabled(false);
262 _systrayCB->setEnabled(false);
263 _onlyShowInKdeCB->setEnabled( false );
264 _hiddenEntryCB->setEnabled( false );
265 _nameLabel->setEnabled(false);
266 _descriptionLabel->setEnabled(false);
267 _commentLabel->setEnabled(false);
268 _execLabel->setEnabled(false);
269 _path_group->setEnabled(false);
270 _term_group->setEnabled(false);
271 _uid_group->setEnabled(false);
272 _iconButton->setEnabled(false);
273 // key binding part
274 general_group_keybind->setEnabled( false );
277 void BasicTab::enableWidgets(bool isDF, bool isDeleted)
279 // set only basic attributes if it is not a .desktop file
280 _nameEdit->setEnabled(!isDeleted);
281 _descriptionEdit->setEnabled(!isDeleted);
282 _commentEdit->setEnabled(!isDeleted);
283 _iconButton->setEnabled(!isDeleted);
284 _execEdit->setEnabled(isDF && !isDeleted);
285 _launchCB->setEnabled(isDF && !isDeleted);
286 _systrayCB->setEnabled(isDF && !isDeleted);
287 _onlyShowInKdeCB->setEnabled( isDF && !isDeleted );
288 _hiddenEntryCB->setEnabled( isDF && !isDeleted );
289 _nameLabel->setEnabled(!isDeleted);
290 _descriptionLabel->setEnabled(!isDeleted);
291 _commentLabel->setEnabled(!isDeleted);
292 _execLabel->setEnabled(isDF && !isDeleted);
294 _path_group->setEnabled(isDF && !isDeleted);
295 _term_group->setEnabled(isDF && !isDeleted);
296 _uid_group->setEnabled(isDF && !isDeleted);
297 general_group_keybind->setEnabled( isDF && !isDeleted );
299 _termOptEdit->setEnabled(isDF && !isDeleted && _terminalCB->isChecked());
300 _termOptLabel->setEnabled(isDF && !isDeleted && _terminalCB->isChecked());
302 _uidEdit->setEnabled(isDF && !isDeleted && _uidCB->isChecked());
303 _uidLabel->setEnabled(isDF && !isDeleted && _uidCB->isChecked());
306 void BasicTab::setFolderInfo(MenuFolderInfo *folderInfo)
308 blockSignals(true);
309 _menuFolderInfo = folderInfo;
310 _menuEntryInfo = 0;
312 _nameEdit->setText(folderInfo->caption);
313 _descriptionEdit->setText(folderInfo->genericname);
314 _descriptionEdit->setCursorPosition(0);
315 _commentEdit->setText(folderInfo->comment);
316 _commentEdit->setCursorPosition(0);
317 _iconButton->setIcon(folderInfo->icon);
319 // clean all disabled fields and return
320 _execEdit->lineEdit()->clear();
321 _pathEdit->lineEdit()->clear();
322 _termOptEdit->clear();
323 _uidEdit->clear();
324 _launchCB->setChecked(false);
325 _systrayCB->setChecked(false);
326 _terminalCB->setChecked(false);
327 _onlyShowInKdeCB->setChecked( false );
328 _hiddenEntryCB->setChecked( false );
329 _uidCB->setChecked(false);
330 _keyEdit->clearKeySequence();
332 enableWidgets(false, folderInfo->hidden);
333 blockSignals(false);
336 void BasicTab::setEntryInfo(MenuEntryInfo *entryInfo)
338 blockSignals(true);
339 _menuFolderInfo = 0;
340 _menuEntryInfo = entryInfo;
342 if (!entryInfo)
344 _nameEdit->clear();
345 _descriptionEdit->clear();
346 _commentEdit->clear();
347 _iconButton->setIcon( QString() );
349 // key binding part
350 _keyEdit->clearKeySequence();
352 _execEdit->lineEdit()->clear();
353 _systrayCB->setChecked(false);
354 _onlyShowInKdeCB->setChecked( false );
355 _hiddenEntryCB->setChecked( false );
357 _pathEdit->lineEdit()->clear();
358 _termOptEdit->clear();
359 _uidEdit->clear();
361 _launchCB->setChecked(false);
362 _terminalCB->setChecked(false);
363 _uidCB->setChecked(false);
364 enableWidgets(true, true);
365 blockSignals(false);
366 return;
369 KDesktopFile *df = entryInfo->desktopFile();
371 _nameEdit->setText(df->readName());
372 _descriptionEdit->setText(df->readGenericName());
373 _descriptionEdit->setCursorPosition(0);
374 _commentEdit->setText(df->readComment());
375 _commentEdit->setCursorPosition(0);
376 _iconButton->setIcon(df->readIcon());
378 // key binding part
379 #ifndef Q_WS_WIN
380 if( KHotKeys::present())
382 if ( !entryInfo->shortcut().isEmpty() )
383 _keyEdit->setKeySequence( entryInfo->shortcut().primary() );
384 else
385 _keyEdit->clearKeySequence();
387 #endif
388 QString temp = df->desktopGroup().readEntry("Exec");
389 if (temp.startsWith("ksystraycmd "))
391 _execEdit->lineEdit()->setText(temp.right(temp.length()-12));
392 _systrayCB->setChecked(true);
394 else
396 _execEdit->lineEdit()->setText(temp);
397 _systrayCB->setChecked(false);
400 _pathEdit->lineEdit()->setText(df->readPath());
401 _termOptEdit->setText(df->desktopGroup().readEntry("TerminalOptions"));
402 _uidEdit->setText(df->desktopGroup().readEntry("X-KDE-Username"));
404 if( df->desktopGroup().hasKey( "StartupNotify" ))
405 _launchCB->setChecked(df->desktopGroup().readEntry("StartupNotify", true));
406 else // backwards comp.
407 _launchCB->setChecked(df->desktopGroup().readEntry("X-KDE-StartupNotify", true));
409 _onlyShowInKdeCB->setChecked( false );
410 if ( df->desktopGroup().hasKey( "OnlyShowIn") )
412 if ( df->desktopGroup().readXdgListEntry("OnlyShowIn").contains( "KDE" ) )
413 _onlyShowInKdeCB->setChecked( true );
416 if ( df->desktopGroup().hasKey( "NoDisplay" ) )
417 _hiddenEntryCB->setChecked( df->desktopGroup().readEntry( "NoDisplay", true ) );
418 else
419 _hiddenEntryCB->setChecked( false );
421 if(df->desktopGroup().readEntry("Terminal", 0) == 1)
422 _terminalCB->setChecked(true);
423 else
424 _terminalCB->setChecked(false);
426 _uidCB->setChecked(df->desktopGroup().readEntry("X-KDE-SubstituteUID", false));
428 enableWidgets(true, entryInfo->hidden);
429 blockSignals(false);
432 void BasicTab::apply()
434 if (_menuEntryInfo)
436 _menuEntryInfo->setDirty();
437 _menuEntryInfo->setCaption(_nameEdit->text());
438 _menuEntryInfo->setDescription(_descriptionEdit->text());
439 _menuEntryInfo->setIcon(_iconButton->icon());
441 KDesktopFile *df = _menuEntryInfo->desktopFile();
442 KConfigGroup dg = df->desktopGroup();
443 dg.writeEntry("Comment", _commentEdit->text());
444 if (_systrayCB->isChecked())
445 dg.writeEntry("Exec", _execEdit->lineEdit()->text().prepend("ksystraycmd "));
446 else
447 dg.writeEntry("Exec", _execEdit->lineEdit()->text());
449 dg.writePathEntry("Path", _pathEdit->lineEdit()->text());
451 if (_terminalCB->isChecked())
452 dg.writeEntry("Terminal", 1);
453 else
454 dg.writeEntry("Terminal", 0);
456 dg.writeEntry("TerminalOptions", _termOptEdit->text());
457 dg.writeEntry("X-KDE-SubstituteUID", _uidCB->isChecked());
458 dg.writeEntry("X-KDE-Username", _uidEdit->text());
459 dg.writeEntry("StartupNotify", _launchCB->isChecked());
460 dg.writeEntry( "NoDisplay", _hiddenEntryCB->isChecked() );
461 if ( _onlyShowInKdeCB->isChecked() )
462 dg.writeXdgListEntry( "OnlyShowIn", QStringList()<<"KDE" );
464 else
466 _menuFolderInfo->setCaption(_nameEdit->text());
467 _menuFolderInfo->setGenericName(_descriptionEdit->text());
468 _menuFolderInfo->setComment(_commentEdit->text());
469 _menuFolderInfo->setIcon(_iconButton->icon());
473 void BasicTab::slotChanged()
475 if (signalsBlocked())
476 return;
477 apply();
478 if (_menuEntryInfo)
479 emit changed( _menuEntryInfo );
480 else
481 emit changed( _menuFolderInfo );
484 void BasicTab::launchcb_clicked()
486 slotChanged();
489 void BasicTab::systraycb_clicked()
491 slotChanged();
494 void BasicTab::onlyshowcb_clicked()
496 slotChanged();
499 void BasicTab::hiddenentrycb_clicked()
501 slotChanged();
504 void BasicTab::termcb_clicked()
506 _termOptEdit->setEnabled(_terminalCB->isChecked());
507 _termOptLabel->setEnabled(_terminalCB->isChecked());
508 slotChanged();
511 void BasicTab::uidcb_clicked()
513 _uidEdit->setEnabled(_uidCB->isChecked());
514 _uidLabel->setEnabled(_uidCB->isChecked());
515 slotChanged();
518 void BasicTab::slotExecSelected()
520 QString path = _execEdit->lineEdit()->text();
521 if (!path.startsWith('\''))
522 _execEdit->lineEdit()->setText(KShell::quoteArg(path));
525 void BasicTab::slotCapturedKeySequence(const QKeySequence& seq)
527 if (signalsBlocked())
528 return;
529 KShortcut cut(seq, QKeySequence());
530 #ifndef Q_WS_WIN
531 if (_menuEntryInfo->isShortcutAvailable( cut ) && KHotKeys::present() )
533 _menuEntryInfo->setShortcut( cut );
535 else
537 // We will not assign the shortcut so reset the visible key sequence
538 _keyEdit->setKeySequence(QKeySequence());
540 #endif
541 if (_menuEntryInfo)
542 emit changed( _menuEntryInfo );
546 void BasicTab::updateHiddenEntry( bool _hidden )
548 if ( _hidden )
549 _hiddenEntryCB->show();
550 else
551 _hiddenEntryCB->hide();