Ran qt3to4
[basket4.git] / src / focusedwidgets.cpp
blob496c5fab8233829058b8464651165e294dc3926f
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 <q3popupmenu.h>
22 //Added by qt3to4:
23 #include <QWheelEvent>
24 #include <QKeyEvent>
25 #include <klocale.h>
27 #include <iostream>
29 #include "focusedwidgets.h"
30 #include "bnpview.h"
31 #include "global.h"
32 #include "basket.h"
34 #ifdef KeyPress
35 #undef KeyPress
36 #endif
37 #include <qevent.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()
52 /**
53 * Thanks to alex.theel@gmx.net, author of TuxCards
54 * Code copied from tuxcards-1.2/src/gui/editor/editor.cpp
56 ***
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();
74 int paragraph;
75 int index;
76 getCursorPosition(&paragraph, &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)
83 insert(" ");
85 KTextEdit::paste();
87 if (preventAutoBullet) {
88 int paragraph2;
89 int index2;
90 getCursorPosition(&paragraph2, &index2);
91 setSelection(paragraph, index, paragraph, index + 1);
92 removeSelectedText();
93 if (paragraph == paragraph2) // We removed one character in that paragraph, so we should move the cursor back to old position... minus one character
94 index2--;
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();
114 if (!clipboard)
115 return;
117 if ( (textFormat() == Qt::RichText) && (!clipboard->data(mode)->provides("application/x-qrichtext")) ) {
118 QString text = clipboard->text(mode);
119 if (text) {
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();
137 return;
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) {
154 int para;
155 int index;
156 getCursorPosition(&para, &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
163 else
164 ensureCursorVisible();
165 updateContents();
169 void FocusedTextEdit::wheelEvent(QWheelEvent *event)
171 if (event->delta() > 0 && contentsY() > 0) {
172 KTextEdit::wheelEvent(event);
173 return;
174 } else if (event->delta() < 0 && contentsY() + visibleHeight() < contentsHeight()) {
175 KTextEdit::wheelEvent(event);
176 return;
177 } else
178 Global::bnpView->currentBasket()->wheelEvent(event);
181 void FocusedTextEdit::enterEvent(QEvent *event)
183 emit mouseEntered();
184 KTextEdit::enterEvent(event);
187 Q3PopupMenu* FocusedTextEdit::createPopupMenu(const QPoint &pos)
189 Q3PopupMenu *menu = KTextEdit::createPopupMenu(pos);
191 int index = 0;
192 int id = 0;
193 while (true) {
194 id = menu->idAt(index);
195 if (id == -1)
196 break;
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);
203 index++;
206 // And return the menu:
207 return 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();
227 else
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();
248 else
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();
269 else
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();
288 else
289 KLineEdit::keyPressEvent(event);
292 void FocusedLineEdit::enterEvent(QEvent *event)
294 emit mouseEntered();
295 KLineEdit::enterEvent(event);
298 #include "focusedwidgets.moc"