Ran qt3to4
[basket4.git] / src / noteedit.cpp
blob445040bfca1824be74b8f0fc749f3ae5a7ccca8e
1 /***************************************************************************
2 * Copyright (C) 2003 by S�astien Laot *
3 * slaout@linux62.org *
4 * *
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. *
9 * *
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. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <qlabel.h>
22 #include <qlayout.h>
23 #include <qlineedit.h>
24 //Added by qt3to4:
25 #include <Q3HBoxLayout>
26 #include <QKeyEvent>
27 #include <Q3GridLayout>
28 #include <klineedit.h>
29 #include <kurlrequester.h>
30 #include <kcolordialog.h>
31 #include <kservice.h>
32 #include <kconfig.h>
33 #include <kmessagebox.h>
34 #include <klocale.h>
35 #include <kmainwindow.h>
36 #include <ktoolbar.h>
37 #include <kaction.h>
38 #include <kurifilter.h>
39 #include <kdebug.h>
40 #include <kstdaction.h>
41 #include <kglobalsettings.h>
43 #include "kicondialog.h"
44 #include "noteedit.h"
45 #include "notecontent.h"
46 // Use Tools::
47 #include "notefactory.h"
48 #include "note.h"
49 #include "basket.h"
50 #include "settings.h"
51 #include "tools.h"
52 #include "variouswidgets.h"
53 #include "focusedwidgets.h"
55 #include <iostream>
57 /** class NoteEditor: */
59 NoteEditor::NoteEditor(NoteContent *noteContent)
61 m_isEmpty = false;
62 m_canceled = false;
63 m_widget = 0;
64 m_textEdit = 0;
65 m_lineEdit = 0;
66 m_noteContent = noteContent;
69 Note* NoteEditor::note()
71 return m_noteContent->note();
74 NoteEditor* NoteEditor::editNoteContent(NoteContent *noteContent, QWidget *parent)
76 TextContent *textContent = dynamic_cast<TextContent*>(noteContent);
77 if (textContent)
78 return new TextEditor(textContent, parent);
80 HtmlContent *htmlContent = dynamic_cast<HtmlContent*>(noteContent);
81 if (htmlContent)
82 return new HtmlEditor(htmlContent, parent);
84 ImageContent *imageContent = dynamic_cast<ImageContent*>(noteContent);
85 if (imageContent)
86 return new ImageEditor(imageContent, parent);
88 AnimationContent *animationContent = dynamic_cast<AnimationContent*>(noteContent);
89 if (animationContent)
90 return new AnimationEditor(animationContent, parent);
92 FileContent *fileContent = dynamic_cast<FileContent*>(noteContent); // Same for SoundContent
93 if (fileContent)
94 return new FileEditor(fileContent, parent);
96 LinkContent *linkContent = dynamic_cast<LinkContent*>(noteContent);
97 if (linkContent)
98 return new LinkEditor(linkContent, parent);
100 LauncherContent *launcherContent = dynamic_cast<LauncherContent*>(noteContent);
101 if (launcherContent)
102 return new LauncherEditor(launcherContent, parent);
104 ColorContent *colorContent = dynamic_cast<ColorContent*>(noteContent);
105 if (colorContent)
106 return new ColorEditor(colorContent, parent);
108 UnknownContent *unknownContent = dynamic_cast<UnknownContent*>(noteContent);
109 if (unknownContent)
110 return new UnknownEditor(unknownContent, parent);
112 return 0;
115 void NoteEditor::setInlineEditor(QWidget *inlineEditor)
117 m_widget = inlineEditor;
118 m_textEdit = 0;
119 m_lineEdit = 0;
121 KTextEdit *textEdit = dynamic_cast<KTextEdit*>(inlineEditor);
122 if (textEdit)
123 m_textEdit = textEdit;
124 else {
125 QLineEdit *lineEdit = dynamic_cast<QLineEdit*>(inlineEditor);
126 if (lineEdit)
127 m_lineEdit = lineEdit;
131 #include <iostream>
133 /** class TextEditor: */
135 TextEditor::TextEditor(TextContent *textContent, QWidget *parent)
136 : NoteEditor(textContent), m_textContent(textContent)
138 FocusedTextEdit *textEdit = new FocusedTextEdit(/*disableUpdatesOnKeyPress=*/true, parent);
139 textEdit->setLineWidth(0);
140 textEdit->setMidLineWidth(0);
141 textEdit->setTextFormat(Qt::PlainText);
142 textEdit->setPaletteBackgroundColor(note()->backgroundColor());
143 textEdit->setPaletteForegroundColor(note()->textColor());
144 textEdit->setFont(note()->font());
145 textEdit->setHScrollBarMode(Q3ScrollView::AlwaysOff);
146 if (Settings::spellCheckTextNotes())
147 textEdit->setCheckSpellingEnabled(true);
148 textEdit->setText(m_textContent->text());
149 textEdit->moveCursor(KTextEdit::MoveEnd, false); // FIXME: Sometimes, the cursor flicker at ends before being positionned where clicked (because kapp->processEvents() I think)
150 textEdit->verticalScrollBar()->setCursor(Qt::ArrowCursor);
151 setInlineEditor(textEdit);
152 connect( textEdit, SIGNAL(escapePressed()), this, SIGNAL(askValidation()) );
153 connect( textEdit, SIGNAL(mouseEntered()), this, SIGNAL(mouseEnteredEditorWidget()) );
155 connect( textEdit, SIGNAL(cursorPositionChanged(int, int)), textContent->note()->basket(), SLOT(editorCursorPositionChanged()) );
156 // In case it is a very big note, the top is displayed and Enter is pressed: the cursor is on bottom, we should enure it visible:
157 QTimer::singleShot( 0, textContent->note()->basket(), SLOT(editorCursorPositionChanged()) );
160 TextEditor::~TextEditor()
162 delete widget(); // TODO: delete that in validate(), so we can remove one method
165 void TextEditor::autoSave(bool toFileToo)
167 bool autoSpellCheck = true;
168 if (toFileToo) {
169 if (Settings::spellCheckTextNotes() != textEdit()->checkSpellingEnabled()) {
170 Settings::setSpellCheckTextNotes(textEdit()->checkSpellingEnabled());
171 Settings::saveConfig();
174 autoSpellCheck = textEdit()->checkSpellingEnabled();
175 textEdit()->setCheckSpellingEnabled(false);
178 m_textContent->setText(textEdit()->text());
180 if (toFileToo) {
181 m_textContent->saveToFile();
182 m_textContent->setEdited();
183 textEdit()->setCheckSpellingEnabled(autoSpellCheck);
187 void TextEditor::validate()
189 if (Settings::spellCheckTextNotes() != textEdit()->checkSpellingEnabled()) {
190 Settings::setSpellCheckTextNotes(textEdit()->checkSpellingEnabled());
191 Settings::saveConfig();
194 textEdit()->setCheckSpellingEnabled(false);
195 if (textEdit()->text().isEmpty())
196 setEmpty();
197 m_textContent->setText(textEdit()->text());
198 m_textContent->saveToFile();
199 m_textContent->setEdited();
201 // delete widget();
204 /** class HtmlEditor: */
206 HtmlEditor::HtmlEditor(HtmlContent *htmlContent, QWidget *parent)
207 : NoteEditor(htmlContent), m_htmlContent(htmlContent)
209 FocusedTextEdit *textEdit = new FocusedTextEdit(/*disableUpdatesOnKeyPress=*/true, parent);
210 textEdit->setLineWidth(0);
211 textEdit->setMidLineWidth(0);
212 textEdit->setTextFormat(Qt::RichText);
213 textEdit->setAutoFormatting(Settings::autoBullet() ? Q3TextEdit::AutoAll : Q3TextEdit::AutoNone);
214 textEdit->setPaletteBackgroundColor(note()->backgroundColor());
215 textEdit->setPaletteForegroundColor(note()->textColor());
216 textEdit->setFont(note()->font());
217 textEdit->setHScrollBarMode(Q3ScrollView::AlwaysOff);
218 textEdit->setText(m_htmlContent->html());
219 textEdit->moveCursor(KTextEdit::MoveEnd, false);
220 textEdit->verticalScrollBar()->setCursor(Qt::ArrowCursor);
221 setInlineEditor(textEdit);
223 connect( textEdit, SIGNAL(mouseEntered()), this, SIGNAL(mouseEnteredEditorWidget()) );
224 connect( textEdit, SIGNAL(escapePressed()), this, SIGNAL(askValidation()) );
226 connect( InlineEditors::instance()->richTextFont, SIGNAL(textChanged(const QString&)), textEdit, SLOT(setFamily(const QString&)) );
227 connect( InlineEditors::instance()->richTextFontSize, SIGNAL(sizeChanged(int)), textEdit, SLOT(setPointSize(int)) );
228 connect( InlineEditors::instance()->richTextColor, SIGNAL(activated(const QColor&)), textEdit, SLOT(setColor(const QColor&)) );
230 connect( InlineEditors::instance()->richTextFont, SIGNAL(escapePressed()), textEdit, SLOT(setFocus()) );
231 connect( InlineEditors::instance()->richTextFont, SIGNAL(returnPressed2()), textEdit, SLOT(setFocus()) );
232 connect( InlineEditors::instance()->richTextFont, SIGNAL(activated(int)), textEdit, SLOT(setFocus()) );
234 connect( InlineEditors::instance()->richTextFontSize, SIGNAL(escapePressed()), textEdit, SLOT(setFocus()) );
235 connect( InlineEditors::instance()->richTextFontSize, SIGNAL(returnPressed2()), textEdit, SLOT(setFocus()) );
236 connect( InlineEditors::instance()->richTextFontSize, SIGNAL(activated(int)), textEdit, SLOT(setFocus()) );
238 connect( InlineEditors::instance()->richTextColor, SIGNAL(escapePressed()), textEdit, SLOT(setFocus()) );
239 connect( InlineEditors::instance()->richTextColor, SIGNAL(returnPressed2()), textEdit, SLOT(setFocus()) );
241 connect( textEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorPositionChanged()) );
242 connect( textEdit, SIGNAL(clicked(int, int)), this, SLOT(cursorPositionChanged()) );
243 connect( textEdit, SIGNAL(currentFontChanged(const QFont&)), this, SLOT(fontChanged(const QFont&)) );
244 // connect( textEdit, SIGNAL(currentVerticalAlignmentChanged(VerticalAlignment)), this, SLOT(slotVerticalAlignmentChanged()) );
246 connect( InlineEditors::instance()->richTextBold, SIGNAL(toggled(bool)), textEdit, SLOT(setBold(bool)) );
247 connect( InlineEditors::instance()->richTextItalic, SIGNAL(toggled(bool)), textEdit, SLOT(setItalic(bool)) );
248 connect( InlineEditors::instance()->richTextUnderline, SIGNAL(toggled(bool)), textEdit, SLOT(setUnderline(bool)) );
249 //REMOVE:
250 //connect( InlineEditors::instance()->richTextBold, SIGNAL(activated()), this, SLOT(setBold()) );
251 //connect( InlineEditors::instance()->richTextItalic, SIGNAL(activated()), this, SLOT(setItalic()) );
252 //connect( InlineEditors::instance()->richTextUnderline, SIGNAL(activated()), this, SLOT(setUnderline()) );
253 connect( InlineEditors::instance()->richTextLeft, SIGNAL(activated()), this, SLOT(setLeft()) );
254 connect( InlineEditors::instance()->richTextCenter, SIGNAL(activated()), this, SLOT(setCentered()) );
255 connect( InlineEditors::instance()->richTextRight, SIGNAL(activated()), this, SLOT(setRight()) );
256 connect( InlineEditors::instance()->richTextJustified, SIGNAL(activated()), this, SLOT(setBlock()) );
258 // InlineEditors::instance()->richTextToolBar()->show();
259 cursorPositionChanged();
260 fontChanged(textEdit->currentFont());
261 //QTimer::singleShot( 0, this, SLOT(cursorPositionChanged()) );
262 InlineEditors::instance()->enableRichTextToolBar();
264 connect( InlineEditors::instance()->richTextUndo, SIGNAL(activated()), textEdit, SLOT(undo()) );
265 connect( InlineEditors::instance()->richTextRedo, SIGNAL(activated()), textEdit, SLOT(redo()) );
266 connect( textEdit, SIGNAL(undoAvailable(bool)), InlineEditors::instance()->richTextUndo, SLOT(setEnabled(bool)) );
267 connect( textEdit, SIGNAL(redoAvailable(bool)), InlineEditors::instance()->richTextRedo, SLOT(setEnabled(bool)) );
268 connect( textEdit, SIGNAL(textChanged()), this, SLOT(textChanged()));
269 InlineEditors::instance()->richTextUndo->setEnabled(false);
270 InlineEditors::instance()->richTextRedo->setEnabled(false);
272 connect( textEdit, SIGNAL(cursorPositionChanged(int, int)), htmlContent->note()->basket(), SLOT(editorCursorPositionChanged()) );
273 // In case it is a very big note, the top is displayed and Enter is pressed: the cursor is on bottom, we should enure it visible:
274 QTimer::singleShot( 0, htmlContent->note()->basket(), SLOT(editorCursorPositionChanged()) );
277 void HtmlEditor::cursorPositionChanged()
279 InlineEditors::instance()->richTextFont->setCurrentFont( textEdit()->currentFont().family() );
280 if (InlineEditors::instance()->richTextColor->color() != textEdit()->color())
281 InlineEditors::instance()->richTextColor->setColor( textEdit()->color() );
282 InlineEditors::instance()->richTextBold->setChecked( textEdit()->bold() );
283 InlineEditors::instance()->richTextItalic->setChecked( textEdit()->italic() );
284 InlineEditors::instance()->richTextUnderline->setChecked( textEdit()->underline() );
286 switch (textEdit()->alignment()) {
287 default:
288 case 1/*Qt::AlignLeft*/: InlineEditors::instance()->richTextLeft->setChecked(true); break;
289 case 4/*Qt::AlignCenter*/: InlineEditors::instance()->richTextCenter->setChecked(true); break;
290 case 2/*Qt::AlignRight*/: InlineEditors::instance()->richTextRight->setChecked(true); break;
291 case -8/*Qt::AlignJustify*/: InlineEditors::instance()->richTextJustified->setChecked(true); break;
295 void HtmlEditor::textChanged()
297 // The following is a workaround for an apparent Qt bug.
298 // When I start typing in a textEdit, the undo&redo actions are not enabled until I click
299 // or move the cursor - probably, the signal undoAvailable() is not emitted.
300 // So, I had to intervene and do that manually.
301 InlineEditors::instance()->richTextUndo->setEnabled(textEdit()->isUndoAvailable());
302 InlineEditors::instance()->richTextRedo->setEnabled(textEdit()->isRedoAvailable());
305 void HtmlEditor::fontChanged(const QFont &font)
307 InlineEditors::instance()->richTextFontSize->setFontSize(font.pointSize());
310 /*void HtmlEditor::slotVe<rticalAlignmentChanged(QTextEdit::VerticalAlignment align)
312 QTextEdit::VerticalAlignment align = textEdit()
313 switch (align) {
314 case KTextEdit::AlignSuperScript:
315 InlineEditors::instance()->richTextSuper->setChecked(true);
316 InlineEditors::instance()->richTextSub->setChecked(false);
317 break;
318 case KTextEdit::AlignSubScript:
319 InlineEditors::instance()->richTextSuper->setChecked(false);
320 InlineEditors::instance()->richTextSub->setChecked(true);
321 break;
322 default:
323 InlineEditors::instance()->richTextSuper->setChecked(false);
324 InlineEditors::instance()->richTextSub->setChecked(false);
327 NoteHtmlEditor::buttonToggled(int id) :
328 case 106:
329 if (isOn && m_toolbar->isButtonOn(107))
330 m_toolbar->setButton(107, false);
331 m_text->setVerticalAlignment(isOn ? KTextEdit::AlignSuperScript : KTextEdit::AlignNormal);
332 break;
333 case 107:
334 if (isOn && m_toolbar->isButtonOn(106))
335 m_toolbar->setButton(106, false);
336 m_text->setVerticalAlignment(isOn ? KTextEdit::AlignSubScript : KTextEdit::AlignNormal);
337 break;
340 // REMOVE: These functions are unused - it's now done by direct connection to textEdit
341 //void HtmlEditor::setBold() { textEdit()->setBold( InlineEditors::instance()->richTextBold->isChecked() ); }
342 //void HtmlEditor::setItalic() { textEdit()->setItalic( InlineEditors::instance()->richTextItalic->isChecked() ); }
343 //void HtmlEditor::setUnderline() { textEdit()->setUnderline( InlineEditors::instance()->richTextUnderline->isChecked() ); }
344 void HtmlEditor::setLeft() { textEdit()->setAlignment(Qt::AlignLeft); }
345 void HtmlEditor::setCentered() { textEdit()->setAlignment(Qt::AlignCenter); }
346 void HtmlEditor::setRight() { textEdit()->setAlignment(Qt::AlignRight); }
347 void HtmlEditor::setBlock() { textEdit()->setAlignment(Qt::AlignJustify); }
349 HtmlEditor::~HtmlEditor()
351 delete widget();
354 void HtmlEditor::autoSave(bool toFileToo)
356 m_htmlContent->setHtml(textEdit()->text());
357 if (toFileToo)
358 m_htmlContent->saveToFile();
359 m_htmlContent->setEdited();
362 void HtmlEditor::validate()
364 if (Tools::htmlToText(textEdit()->text()).isEmpty())
365 setEmpty();
366 m_htmlContent->setHtml(textEdit()->text());
367 m_htmlContent->saveToFile();
368 m_htmlContent->setEdited();
370 disconnect();
371 widget()->disconnect();
372 if (InlineEditors::instance())
374 InlineEditors::instance()->disableRichTextToolBar();
375 // if (InlineEditors::instance()->richTextToolBar())
376 // InlineEditors::instance()->richTextToolBar()->hide();
378 delete widget();
379 setInlineEditor(0);
382 /** class ImageEditor: */
384 ImageEditor::ImageEditor(ImageContent *imageContent, QWidget *parent)
385 : NoteEditor(imageContent)
387 int choice = KMessageBox::questionYesNo(parent, i18n(
388 "Images can not be edited here at the moment (the next version of BasKet Note Pads will include an image editor).\n"
389 "Do you want to open it with an application that understand it?"),
390 i18n("Edit Image Note"),
391 KStdGuiItem::open(),
392 KStdGuiItem::cancel());
394 if (choice == KMessageBox::Yes)
395 note()->basket()->noteOpen(note());
398 /** class AnimationEditor: */
400 AnimationEditor::AnimationEditor(AnimationContent *animationContent, QWidget *parent)
401 : NoteEditor(animationContent)
403 int choice = KMessageBox::questionYesNo(parent, i18n(
404 "This animated image can not be edited here.\n"
405 "Do you want to open it with an application that understands it?"),
406 i18n("Edit Animation Note"),
407 KStdGuiItem::open(),
408 KStdGuiItem::cancel());
410 if (choice == KMessageBox::Yes)
411 note()->basket()->noteOpen(note());
414 /** class FileEditor: */
416 FileEditor::FileEditor(FileContent *fileContent, QWidget *parent)
417 : NoteEditor(fileContent), m_fileContent(fileContent)
419 FocusedLineEdit *lineEdit = new FocusedLineEdit(parent);
420 lineEdit->setLineWidth(0);
421 lineEdit->setMidLineWidth(0);
422 lineEdit->setPaletteBackgroundColor(note()->backgroundColor());
423 lineEdit->setPaletteForegroundColor(note()->textColor());
424 lineEdit->setFont(note()->font());
425 lineEdit->setText(m_fileContent->fileName());
426 lineEdit->selectAll();
427 setInlineEditor(lineEdit);
428 connect( lineEdit, SIGNAL(returnPressed()), this, SIGNAL(askValidation()) );
429 connect( lineEdit, SIGNAL(escapePressed()), this, SIGNAL(askValidation()) );
430 connect( lineEdit, SIGNAL(mouseEntered()), this, SIGNAL(mouseEnteredEditorWidget()) );
433 FileEditor::~FileEditor()
435 delete widget();
438 void FileEditor::autoSave(bool toFileToo)
440 // FIXME: How to detect cancel?
441 if (toFileToo && !lineEdit()->text().isEmpty() && m_fileContent->trySetFileName(lineEdit()->text())) {
442 m_fileContent->setFileName(lineEdit()->text());
443 m_fileContent->setEdited();
447 void FileEditor::validate()
449 autoSave(/*toFileToo=*/true);
452 /** class LinkEditor: */
454 LinkEditor::LinkEditor(LinkContent *linkContent, QWidget *parent)
455 : NoteEditor(linkContent)
457 LinkEditDialog dialog(linkContent, parent);
458 if (dialog.exec() == QDialog::Rejected)
459 cancel();
460 if (linkContent->url().isEmpty() && linkContent->title().isEmpty())
461 setEmpty();
464 /** class LauncherEditor: */
466 LauncherEditor::LauncherEditor(LauncherContent *launcherContent, QWidget *parent)
467 : NoteEditor(launcherContent)
469 LauncherEditDialog dialog(launcherContent, parent);
470 if (dialog.exec() == QDialog::Rejected)
471 cancel();
472 if (launcherContent->name().isEmpty() && launcherContent->exec().isEmpty())
473 setEmpty();
476 /** class ColorEditor: */
478 ColorEditor::ColorEditor(ColorContent *colorContent, QWidget *parent)
479 : NoteEditor(colorContent)
481 KColorDialog dialog(parent, /*name=*/"EditColor", /*modal=*/true);
482 dialog.setColor(colorContent->color());
483 dialog.setCaption(i18n("Edit Color Note"));
484 if (dialog.exec() == QDialog::Accepted) {
485 if (dialog.color() != colorContent->color()) {
486 colorContent->setColor(dialog.color());
487 colorContent->setEdited();
489 } else
490 cancel();
492 /* This code don't allow to set a caption to the dialog:
493 QColor color = colorContent()->color();
494 if (KColorDialog::getColor(color, parent) == QDialog::Accepted && color != m_color) {
495 colorContent()->setColor(color);
496 setEdited();
500 /** class UnknownEditor: */
502 UnknownEditor::UnknownEditor(UnknownContent *unknownContent, QWidget *parent)
503 : NoteEditor(unknownContent)
505 KMessageBox::information(parent, i18n(
506 "The type of this note is unknown and can not be edited here.\n"
507 "You however can drag or copy the note into an application that understands it."),
508 i18n("Edit Unknown Note"));
511 /*********************************************************************/
514 /** class DebuggedLineEdit: */
516 DebuggedLineEdit::DebuggedLineEdit(const QString &text, QWidget *parent)
517 : QLineEdit(text, parent)
521 DebuggedLineEdit::~DebuggedLineEdit()
525 void DebuggedLineEdit::keyPressEvent(QKeyEvent *event)
527 QString oldText = text();
528 QLineEdit::keyPressEvent(event);
529 if (oldText != text())
530 emit textChanged(text());
534 /** class LinkEditDialog: */
536 LinkEditDialog::LinkEditDialog(LinkContent *contentNote, QWidget *parent/*, QKeyEvent *ke*/)
537 : KDialogBase(KDialogBase::Plain, i18n("Edit Link Note"), KDialogBase::Ok | KDialogBase::Cancel,
538 KDialogBase::Ok, parent, /*name=*/"EditLink", /*modal=*/true, /*separator=*/true),
539 m_noteContent(contentNote)
541 QWidget *page = plainPage();
542 Q3GridLayout *layout = new Q3GridLayout(page, /*nRows=*/4, /*nCols=*/2, /*margin=*/0, spacingHint());
544 m_url = new KURLRequester(m_noteContent->url().url(), page);
546 QWidget *wid1 = new QWidget(page);
547 Q3HBoxLayout *titleLay = new Q3HBoxLayout(wid1, /*margin=*/0, spacingHint());
548 m_title = new DebuggedLineEdit(m_noteContent->title(), wid1);
549 m_autoTitle = new QPushButton(i18n("Auto"), wid1);
550 m_autoTitle->setToggleButton(true);
551 m_autoTitle->setOn(m_noteContent->autoTitle());
552 titleLay->addWidget(m_title);
553 titleLay->addWidget(m_autoTitle);
555 QWidget *wid = new QWidget(page);
556 Q3HBoxLayout *hLay = new Q3HBoxLayout(wid, /*margin=*/0, spacingHint());
557 m_icon = new KIconButton(wid);
558 QLabel *label3 = new QLabel(m_icon, i18n("&Icon:"), page);
559 KURL filteredURL = NoteFactory::filteredURL(KURL(m_url->lineEdit()->text()));//KURIFilter::self()->filteredURI(KURL(m_url->lineEdit()->text()));
560 m_icon->setIconType(KIcon::NoGroup, KIcon::MimeType);
561 m_icon->setIconSize(LinkLook::lookForURL(filteredURL)->iconSize());
562 m_autoIcon = new QPushButton(i18n("Auto"), wid); // Create before to know size here:
563 /* Icon button: */
564 m_icon->setIcon(m_noteContent->icon());
565 int minSize = m_autoIcon->sizeHint().height();
566 // Make the icon button at least the same heigh than the other buttons for a better alignment (nicer to the eyes):
567 if (m_icon->sizeHint().height() < minSize)
568 m_icon->setFixedSize(minSize, minSize);
569 else
570 m_icon->setFixedSize(m_icon->sizeHint().height(), m_icon->sizeHint().height()); // Make it square
571 /* Auto button: */
572 m_autoIcon->setToggleButton(true);
573 m_autoIcon->setOn(m_noteContent->autoIcon());
574 hLay->addWidget(m_icon);
575 hLay->addWidget(m_autoIcon);
576 hLay->addStretch();
578 m_url->lineEdit()->setMinimumWidth(m_url->lineEdit()->fontMetrics().maxWidth()*20);
579 m_title->setMinimumWidth(m_title->fontMetrics().maxWidth()*20);
581 //m_url->setShowLocalProtocol(true);
582 QLabel *label1 = new QLabel(m_url, i18n("Ta&rget:"), page);
583 QLabel *label2 = new QLabel(m_title, i18n("&Title:"), page);
584 layout->addWidget(label1, 0, 0, Qt::AlignVCenter);
585 layout->addWidget(label2, 1, 0, Qt::AlignVCenter);
586 layout->addWidget(label3, 2, 0, Qt::AlignVCenter);
587 layout->addWidget(m_url, 0, 1, Qt::AlignVCenter);
588 layout->addWidget(wid1, 1, 1, Qt::AlignVCenter);
589 layout->addWidget(wid, 2, 1, Qt::AlignVCenter);
591 m_isAutoModified = false;
592 connect( m_url, SIGNAL(textChanged(const QString&)), this, SLOT(urlChanged(const QString&)) );
593 connect( m_title, SIGNAL(textChanged(const QString&)), this, SLOT(doNotAutoTitle(const QString&)) );
594 connect( m_icon, SIGNAL(iconChanged(QString)) , this, SLOT(doNotAutoIcon(QString)) );
595 connect( m_autoTitle, SIGNAL(clicked()), this, SLOT(guessTitle()) );
596 connect( m_autoIcon, SIGNAL(clicked()), this, SLOT(guessIcon()) );
598 QWidget *stretchWidget = new QWidget(page);
599 stretchWidget->setSizePolicy(QSizePolicy(/*hor=*/QSizePolicy::Fixed, /*ver=*/QSizePolicy::Expanding, /*hStretch=*/1, /*vStretch=*/255)); // Make it fill ALL vertical space
600 layout->addWidget(stretchWidget, 3, 1, Qt::AlignVCenter);
603 urlChanged("");
605 // if (ke)
606 // kapp->postEvent(m_url->lineEdit(), ke);
609 LinkEditDialog::~LinkEditDialog()
613 void LinkEditDialog::polish()
615 KDialogBase::polish();
616 if (m_url->lineEdit()->text().isEmpty()) {
617 m_url->setFocus();
618 m_url->lineEdit()->end(false);
619 } else {
620 m_title->setFocus();
621 m_title->end(false);
626 void LinkEditDialog::urlChanged(const QString&)
628 m_isAutoModified = true;
629 // guessTitle();
630 // guessIcon();
631 // Optimization (filter only once):
632 KURL filteredURL = NoteFactory::filteredURL(KURL(m_url->url()));//KURIFilter::self()->filteredURI(KURL(m_url->url()));
633 if (m_autoIcon->isOn())
634 m_icon->setIcon(NoteFactory::iconForURL(filteredURL));
635 if (m_autoTitle->isOn()) {
636 m_title->setText(NoteFactory::titleForURL(filteredURL));
637 m_autoTitle->setOn(true); // Because the setText() will disable it!
641 void LinkEditDialog::doNotAutoTitle(const QString&)
643 if (m_isAutoModified)
644 m_isAutoModified = false;
645 else
646 m_autoTitle->setOn(false);
649 void LinkEditDialog::doNotAutoIcon(QString)
651 m_autoIcon->setOn(false);
654 void LinkEditDialog::guessIcon()
656 if (m_autoIcon->isOn()) {
657 KURL filteredURL = NoteFactory::filteredURL(KURL(m_url->url()));//KURIFilter::self()->filteredURI(KURL(m_url->url()));
658 m_icon->setIcon(NoteFactory::iconForURL(filteredURL));
662 void LinkEditDialog::guessTitle()
664 if (m_autoTitle->isOn()) {
665 KURL filteredURL = NoteFactory::filteredURL(KURL(m_url->url()));//KURIFilter::self()->filteredURI(KURL(m_url->url()));
666 m_title->setText(NoteFactory::titleForURL(filteredURL));
667 m_autoTitle->setOn(true); // Because the setText() will disable it!
671 void LinkEditDialog::slotOk()
673 KURL filteredURL = NoteFactory::filteredURL(KURL(m_url->url()));//KURIFilter::self()->filteredURI(KURL(m_url->url()));
674 m_noteContent->setLink(filteredURL, m_title->text(), m_icon->icon(), m_autoTitle->isOn(), m_autoIcon->isOn());
675 m_noteContent->setEdited();
677 /* Change icon size if link look have changed */
678 LinkLook *linkLook = LinkLook::lookForURL(filteredURL);
679 QString icon = m_icon->icon(); // When we change size, icon isn't changed and keep it's old size
680 m_icon->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); // Reset size policy
681 m_icon->setIconSize(linkLook->iconSize()); // So I store it's name and reload it after size change !
682 m_icon->setIcon(icon);
683 int minSize = m_autoIcon->sizeHint().height();
684 // Make the icon button at least the same heigh than the other buttons for a better alignment (nicer to the eyes):
685 if (m_icon->sizeHint().height() < minSize)
686 m_icon->setFixedSize(minSize, minSize);
687 else
688 m_icon->setFixedSize(m_icon->sizeHint().height(), m_icon->sizeHint().height()); // Make it square
690 KDialogBase::slotOk();
693 /** class LauncherEditDialog: */
695 LauncherEditDialog::LauncherEditDialog(LauncherContent *contentNote, QWidget *parent)
696 : KDialogBase(KDialogBase::Plain, i18n("Edit Launcher Note"), KDialogBase::Ok | KDialogBase::Cancel,
697 KDialogBase::Ok, parent, /*name=*/"EditLauncher", /*modal=*/true, /*separator=*/true),
698 m_noteContent(contentNote)
700 QWidget *page = plainPage();
701 Q3GridLayout *layout = new Q3GridLayout(page, /*nRows=*/4, /*nCols=*/2, /*margin=*/0, spacingHint());
703 KService service(contentNote->fullPath());
705 m_command = new RunCommandRequester(service.exec(), i18n("Choose a command to run:"), page);
706 m_name = new QLineEdit(service.name(), page);
708 QWidget *wid = new QWidget(page);
709 Q3HBoxLayout *hLay = new Q3HBoxLayout(wid, /*margin=*/0, spacingHint());
710 m_icon = new KIconButton(wid);
711 QLabel *label = new QLabel(m_icon, i18n("&Icon:"), page);
712 m_icon->setIconType(KIcon::NoGroup, KIcon::Application);
713 m_icon->setIconSize(LinkLook::launcherLook->iconSize());
714 QPushButton *guessButton = new QPushButton(i18n("&Guess"), wid);
715 /* Icon button: */
716 m_icon->setIcon(service.icon());
717 int minSize = guessButton->sizeHint().height();
718 // Make the icon button at least the same heigh than the other buttons for a better alignment (nicer to the eyes):
719 if (m_icon->sizeHint().height() < minSize)
720 m_icon->setFixedSize(minSize, minSize);
721 else
722 m_icon->setFixedSize(m_icon->sizeHint().height(), m_icon->sizeHint().height()); // Make it square
723 /* Guess button: */
724 hLay->addWidget(m_icon);
725 hLay->addWidget(guessButton);
726 hLay->addStretch();
728 m_command->lineEdit()->setMinimumWidth(m_command->lineEdit()->fontMetrics().maxWidth()*20);
730 QLabel *label1 = new QLabel(m_command->lineEdit(), i18n("Comman&d:"), page);
731 QLabel *label2 = new QLabel(m_name, i18n("&Name:"), page);
732 layout->addWidget(label1, 0, 0, Qt::AlignVCenter);
733 layout->addWidget(label2, 1, 0, Qt::AlignVCenter);
734 layout->addWidget(label, 2, 0, Qt::AlignVCenter);
735 layout->addWidget(m_command, 0, 1, Qt::AlignVCenter);
736 layout->addWidget(m_name, 1, 1, Qt::AlignVCenter);
737 layout->addWidget(wid, 2, 1, Qt::AlignVCenter);
739 QWidget *stretchWidget = new QWidget(page);
740 stretchWidget->setSizePolicy(QSizePolicy(/*hor=*/QSizePolicy::Fixed, /*ver=*/QSizePolicy::Expanding, /*hStretch=*/1, /*vStretch=*/255)); // Make it fill ALL vertical space
741 layout->addWidget(stretchWidget, 3, 1, Qt::AlignVCenter);
743 connect( guessButton, SIGNAL(clicked()), this, SLOT(guessIcon()) );
746 LauncherEditDialog::~LauncherEditDialog()
750 void LauncherEditDialog::polish()
752 KDialogBase::polish();
753 if (m_command->runCommand().isEmpty()) {
754 m_command->lineEdit()->setFocus();
755 m_command->lineEdit()->end(false);
756 } else {
757 m_name->setFocus();
758 m_name->end(false);
762 void LauncherEditDialog::slotOk()
764 // TODO: Remember if a string has been modified AND IS DIFFERENT FROM THE ORIGINAL!
766 KConfig conf(m_noteContent->fullPath());
767 conf.setGroup("Desktop Entry");
768 conf.writeEntry("Exec", m_command->runCommand());
769 conf.writeEntry("Name", m_name->text());
770 conf.writeEntry("Icon", m_icon->icon());
772 // Just for faster feedback: conf object will save to disk (and then m_note->loadContent() called)
773 m_noteContent->setLauncher(m_name->text(), m_icon->icon(), m_command->runCommand());
774 m_noteContent->setEdited();
776 KDialogBase::slotOk();
779 void LauncherEditDialog::guessIcon()
781 m_icon->setIcon( NoteFactory::iconForCommand(m_command->runCommand()) );
784 /** class InlineEditors: */
786 InlineEditors::InlineEditors()
790 InlineEditors::~InlineEditors()
794 InlineEditors* InlineEditors::instance()
796 static InlineEditors *instance = 0;
797 if (!instance)
798 instance = new InlineEditors();
799 return instance;
802 void InlineEditors::initToolBars(KActionCollection *actionCollection)
804 QFont defaultFont;
805 QColor textColor = (Global::bnpView && Global::bnpView->currentBasket() ?
806 Global::bnpView->currentBasket()->textColor() :
807 KGlobalSettings::textColor());
809 // Init the RichTextEditor Toolbar:
810 richTextFont = new FocusedFontCombo(Global::mainWindow());
811 richTextFont->setFixedWidth(richTextFont->sizeHint().width() * 2 / 3);
812 richTextFont->setCurrentFont(defaultFont.family());
813 KWidgetAction *action = new KWidgetAction(richTextFont, i18n("Font"), Qt::Key_F6,
814 /*receiver=*/0, /*slot=*/"", actionCollection, "richtext_font");
816 richTextFontSize = new FontSizeCombo(/*rw=*/true, Global::mainWindow());
817 richTextFontSize->setFontSize(defaultFont.pointSize());
818 action = new KWidgetAction(richTextFontSize, i18n("Font Size"), Qt::Key_F7,
819 /*receiver=*/0, /*slot=*/"", actionCollection, "richtext_font_size");
821 richTextColor = new FocusedColorCombo(Global::mainWindow());
822 richTextColor->setFixedWidth(richTextColor->sizeHint().height() * 2);
823 richTextColor->setColor(textColor);
824 action = new KWidgetAction(richTextColor, i18n("Color"), KShortcut(), 0, SLOT(), actionCollection, "richtext_color");
826 richTextBold = new KToggleAction( i18n("Bold"), "text_bold", "Ctrl+B", actionCollection, "richtext_bold" );
827 richTextItalic = new KToggleAction( i18n("Italic"), "text_italic", "Ctrl+I", actionCollection, "richtext_italic" );
828 richTextUnderline = new KToggleAction( i18n("Underline"), "text_under", "Ctrl+U", actionCollection, "richtext_underline" );
830 // richTextSuper = new KToggleAction( i18n("Superscript"), "text_super", "", actionCollection, "richtext_super" );
831 // richTextSub = new KToggleAction( i18n("Subscript"), "text_sub", "", actionCollection, "richtext_sub" );
833 richTextLeft = new KToggleAction( i18n("Align Left"), "text_left", "", actionCollection, "richtext_left" );
834 richTextCenter = new KToggleAction( i18n("Centered"), "text_center", "", actionCollection, "richtext_center" );
835 richTextRight = new KToggleAction( i18n("Align Right"), "text_right", "", actionCollection, "richtext_right" );
836 richTextJustified = new KToggleAction( i18n("Justified"), "text_block", "", actionCollection, "richtext_block" );
838 richTextLeft->setExclusiveGroup("rt_justify");
839 richTextCenter->setExclusiveGroup("rt_justify");
840 richTextRight->setExclusiveGroup("rt_justify");
841 richTextJustified->setExclusiveGroup("rt_justify");
843 richTextUndo = new KAction( i18n("Undo"), "undo", "", actionCollection, "richtext_undo");
844 richTextRedo = new KAction( i18n("Redo"), "redo", "", actionCollection, "richtext_redo");
846 disableRichTextToolBar();
849 KToolBar* InlineEditors::richTextToolBar()
851 if (Global::mainWindow()) {
852 Global::mainWindow()->toolBar(); // Make sure we create the main toolbar FIRST, so it will be on top of the edit toolbar!
853 return Global::mainWindow()->toolBar("richTextEditToolBar");
854 } else
855 return 0;
858 void InlineEditors::enableRichTextToolBar()
860 richTextFont->setEnabled(true);
861 richTextFontSize->setEnabled(true);
862 richTextColor->setEnabled(true);
863 richTextBold->setEnabled(true);
864 richTextItalic->setEnabled(true);
865 richTextUnderline->setEnabled(true);
866 richTextLeft->setEnabled(true);
867 richTextCenter->setEnabled(true);
868 richTextRight->setEnabled(true);
869 richTextJustified->setEnabled(true);
870 richTextUndo->setEnabled(true);
871 richTextRedo->setEnabled(true);
874 void InlineEditors::disableRichTextToolBar()
876 disconnect(richTextFont);
877 disconnect(richTextFontSize);
878 disconnect(richTextColor);
879 disconnect(richTextBold);
880 disconnect(richTextItalic);
881 disconnect(richTextUnderline);
882 disconnect(richTextLeft);
883 disconnect(richTextCenter);
884 disconnect(richTextRight);
885 disconnect(richTextJustified);
886 disconnect(richTextUndo);
887 disconnect(richTextRedo);
889 richTextFont->setEnabled(false);
890 richTextFontSize->setEnabled(false);
891 richTextColor->setEnabled(false);
892 richTextBold->setEnabled(false);
893 richTextItalic->setEnabled(false);
894 richTextUnderline->setEnabled(false);
895 richTextLeft->setEnabled(false);
896 richTextCenter->setEnabled(false);
897 richTextRight->setEnabled(false);
898 richTextJustified->setEnabled(false);
899 richTextUndo->setEnabled(false);
900 richTextRedo->setEnabled(false);
902 // Return to a "proper" state:
903 QFont defaultFont;
904 QColor textColor = (Global::bnpView && Global::bnpView->currentBasket() ?
905 Global::bnpView->currentBasket()->textColor() :
906 KGlobalSettings::textColor());
907 richTextFont->setCurrentFont(defaultFont.family());
908 richTextFontSize->setFontSize(defaultFont.pointSize());
909 richTextColor->setColor(textColor);
910 richTextBold->setChecked(false);
911 richTextItalic->setChecked(false);
912 richTextUnderline->setChecked(false);
913 richTextLeft->setChecked(false);
914 richTextCenter->setChecked(false);
915 richTextRight->setChecked(false);
916 richTextJustified->setChecked(false);
919 #include "noteedit.moc"