1 /***************************************************************************
2 * Copyright (C) 2003 by S�astien Laot *
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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <q3popupmenu.h>
23 #include <QWheelEvent>
29 #include "focusedwidgets.h"
39 /** class FocusedTextEdit */
41 FocusedTextEdit::FocusedTextEdit(bool disableUpdatesOnKeyPress
, QWidget
*parent
, const char *name
)
42 : KTextEdit(parent
, name
),
43 m_disableUpdatesOnKeyPress(disableUpdatesOnKeyPress
)
45 setWFlags(Qt::WNoAutoErase
); // Does not work, we still need the disableUpdatesOnKeyPress hack!
48 FocusedTextEdit::~FocusedTextEdit()
53 * Thanks to alex.theel@gmx.net, author of TuxCards
54 * Code copied from tuxcards-1.2/src/gui/editor/editor.cpp
57 * Override the regular paste() methode, so that lines are
58 * not separated by each other with an blank line.
60 void FocusedTextEdit::paste()
62 adaptClipboardText(QClipboard::Selection
);
63 adaptClipboardText(QClipboard::Clipboard
);
65 // If we paste a application/x-qrichtext content starting with a "-" or a "*",
66 // then auto-bulletting will crash.
67 // So we insert a space to be sure what we paste will not trigger the auto-bulleting.
69 // enum AutoFormatting { AutoNone = 0, AutoBulletList = 0x00000001, AutoAll = 0xffffffff }
70 // uint oldAutoFormating = autoFormatting();
71 // setAutoFormatting(AutoNone);
73 QClipboard
*clipboard
= QApplication::clipboard();
76 getCursorPosition(¶graph
, &index
);
78 bool preventAutoBullet
= (index
== 0) &&
79 (clipboard
->data(QClipboard::Selection
)->provides("application/x-qrichtext") ||
80 clipboard
->data(QClipboard::Clipboard
)->provides("application/x-qrichtext") );
82 if (preventAutoBullet
)
87 if (preventAutoBullet
) {
90 getCursorPosition(¶graph2
, &index2
);
91 setSelection(paragraph
, index
, paragraph
, index
+ 1);
93 if (paragraph
== paragraph2
) // We removed one character in that paragraph, so we should move the cursor back to old position... minus one character
95 setCursorPosition(paragraph2
, index2
);
99 // setAutoFormatting(oldAutoFormating);
103 * Thanks to alex.theel@gmx.net, author of TuxCards
104 * Code copied from tuxcards-1.2/src/gui/editor/editor.cpp
107 * Auxiliar method that takes the text from the clipboard - using the
108 * specified 'mode' -, replaces all '\n' within that text and writes
109 * it back to the clipboard.
111 void FocusedTextEdit::adaptClipboardText(QClipboard::Mode mode
)
113 QClipboard
*clipboard
= QApplication::clipboard();
117 if ( (textFormat() == Qt::RichText
) && (!clipboard
->data(mode
)->provides("application/x-qrichtext")) ) {
118 QString text
= clipboard
->text(mode
);
120 text
= text
.replace("\n", QChar(0x2028));
121 clipboard
->setText(text
, mode
);
127 QTextCursor
* FocusedTextEdit::textCursor() const
129 return KTextEdit::textCursor();
133 void FocusedTextEdit::keyPressEvent(QKeyEvent
*event
)
135 if (event
->key() == Qt::Key_Escape
) {
136 emit
escapePressed();
138 // In RichTextFormat mode, [Return] create a new paragraphe.
139 // To keep consistency with TextFormat mode (new line on [Return]),
140 // we redirect [Return] to simulate [Ctrl+Return] (create a new line in both modes).
141 // Create new paragraphes still possible in RichTextFormat mode with [Shift+Enter].
142 } else if (event
->key() == Qt::Key_Return
&& event
->state() == 0)
143 event
= new QKeyEvent(QEvent::KeyPress
, event
->key(), event
->ascii(), Qt::ControlButton
,
144 event
->text(), event
->isAutoRepeat(), event
->count() );
145 else if (event
->key() == Qt::Key_Return
&& event
->state() & Qt::ControlButton
)
146 event
= new QKeyEvent(QEvent::KeyPress
, event
->key(), event
->ascii(), Qt::ShiftButton
,
147 event
->text(), event
->isAutoRepeat(), event
->count() );
149 if (m_disableUpdatesOnKeyPress
)
150 setUpdatesEnabled(false);
151 KTextEdit::keyPressEvent(event
);
152 // Workarround (for ensuring the cursor to be visible): signal not emited when pressing those keys:
153 if (event
->key() == Qt::Key_Home
|| event
->key() == Qt::Key_End
|| event
->key() == Qt::Key_PageUp
|| event
->key() == Qt::Key_PageDown
) {
156 getCursorPosition(¶
, &index
);
157 emit
cursorPositionChanged(para
, index
);
159 if (m_disableUpdatesOnKeyPress
) {
160 setUpdatesEnabled(true);
161 if (text().isEmpty())
162 ;// emit textChanged(); // TODO: DOESN'T WORK: the editor is not resized down to only one line of text
164 ensureCursorVisible();
169 void FocusedTextEdit::wheelEvent(QWheelEvent
*event
)
171 if (event
->delta() > 0 && contentsY() > 0) {
172 KTextEdit::wheelEvent(event
);
174 } else if (event
->delta() < 0 && contentsY() + visibleHeight() < contentsHeight()) {
175 KTextEdit::wheelEvent(event
);
178 Global::bnpView
->currentBasket()->wheelEvent(event
);
181 void FocusedTextEdit::enterEvent(QEvent
*event
)
184 KTextEdit::enterEvent(event
);
187 Q3PopupMenu
* FocusedTextEdit::createPopupMenu(const QPoint
&pos
)
189 Q3PopupMenu
*menu
= KTextEdit::createPopupMenu(pos
);
194 id
= menu
->idAt(index
);
197 // Disable Spell Check for rich text editors, because it doesn't work anyway:
198 if (textFormat() == Qt::RichText
&& (menu
->text(id
) == i18n("Auto Spell Check") || menu
->text(id
) == i18n("Check Spelling...")))
199 menu
->setItemEnabled(id
, false);
200 // Always enable tabulations!:
201 if (menu
->text(id
) == i18n("Allow Tabulations"))
202 menu
->setItemEnabled(id
, false);
206 // And return the menu:
210 /** class FocusedColorCombo: */
212 FocusedColorCombo::FocusedColorCombo(QWidget
*parent
, const char *name
)
213 : KColorCombo(parent
, name
)
217 FocusedColorCombo::~FocusedColorCombo()
221 void FocusedColorCombo::keyPressEvent(QKeyEvent
*event
)
223 if (event
->key() == Qt::Key_Escape
)
224 emit
escapePressed();
225 else if (event
->key() == Qt::Key_Return
)
226 emit
returnPressed2();
228 KColorCombo::keyPressEvent(event
);
231 /** class FocusedFontCombo: */
233 FocusedFontCombo::FocusedFontCombo(QWidget
*parent
, const char *name
)
234 : KFontCombo(parent
, name
)
238 FocusedFontCombo::~FocusedFontCombo()
242 void FocusedFontCombo::keyPressEvent(QKeyEvent
*event
)
244 if (event
->key() == Qt::Key_Escape
)
245 emit
escapePressed();
246 else if (event
->key() == Qt::Key_Return
)
247 emit
returnPressed2();
249 KFontCombo::keyPressEvent(event
);
252 /** class FocusedComboBox: */
254 FocusedComboBox::FocusedComboBox(QWidget
*parent
, const char *name
)
255 : KComboBox(parent
, name
)
259 FocusedComboBox::~FocusedComboBox()
263 void FocusedComboBox::keyPressEvent(QKeyEvent
*event
)
265 if (event
->key() == Qt::Key_Escape
)
266 emit
escapePressed();
267 else if (event
->key() == Qt::Key_Return
)
268 emit
returnPressed2();
270 KComboBox::keyPressEvent(event
);
273 /** class FocusedLineEdit: */
275 FocusedLineEdit::FocusedLineEdit(QWidget
*parent
, const char *name
)
276 : KLineEdit(parent
, name
)
280 FocusedLineEdit::~FocusedLineEdit()
284 void FocusedLineEdit::keyPressEvent(QKeyEvent
*event
)
286 if (event
->key() == Qt::Key_Escape
)
287 emit
escapePressed();
289 KLineEdit::keyPressEvent(event
);
292 void FocusedLineEdit::enterEvent(QEvent
*event
)
295 KLineEdit::enterEvent(event
);
298 #include "focusedwidgets.moc"