root/trunk/qt4-gui/src/widgets/mledit.cpp

Revision 6451, 8.5 kB (checked in by flynd, 5 months ago)

No need to use a separate signal for fixed font changes.

  • Property svn:eol-style set to native
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of Licq, an instant messaging client for UNIX.
4 * Copyright (C) 1999-2006 Licq developers
5 *
6 * Licq is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Licq is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Licq; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21// written by Graham Roff <graham@licq.org>
22// contributions by Dirk A. Mueller <dirk@licq.org>
23
24#include "mledit.h"
25
26#include <QKeyEvent>
27#include <QMenu>
28
29#include "config/general.h"
30
31using namespace LicqQtGui;
32/* TRANSLATOR LicqQtGui::MLEdit */
33
34MLEdit::MLEdit(bool wordWrap, QWidget* parent, bool useFixedFont, const char* name)
35  : MLEDIT_BASE(parent),
36    myUseFixedFont(useFixedFont),
37    myFixSetTextNewlines(true),
38    myLastKeyWasReturn(false),
39    myLinesHint(0)
40{
41  setObjectName(name);
42  setAcceptRichText(false);
43  setTabChangesFocus(true);
44
45  if (!wordWrap)
46    setLineWrapMode(NoWrap);
47
48  updateFont();
49  connect(Config::General::instance(), SIGNAL(fontChanged()), SLOT(updateFont()));
50}
51
52MLEdit::~MLEdit()
53{
54  // Empty
55}
56
57void MLEdit::appendNoNewLine(const QString& s)
58{
59  GotoEnd();
60  insertPlainText(s);
61}
62
63void MLEdit::GotoEnd()
64{
65  moveCursor(QTextCursor::End);
66}
67
68void MLEdit::setBackground(const QColor& color)
69{
70  QPalette pal = palette();
71
72  pal.setColor(QPalette::Active, QPalette::Base, color);
73  pal.setColor(QPalette::Inactive, QPalette::Base, color);
74
75  setPalette(pal);
76}
77
78void MLEdit::setForeground(const QColor& color)
79{
80  QPalette pal = palette();
81
82  pal.setColor(QPalette::Active, QPalette::Text, color);
83  pal.setColor(QPalette::Inactive, QPalette::Text, color);
84
85  setPalette(pal);
86}
87
88void MLEdit::keyPressEvent(QKeyEvent* event)
89{
90  const bool isShift   = event->modifiers() & Qt::ShiftModifier;
91  const bool isControl = event->modifiers() & Qt::ControlModifier;
92
93  QTextCursor cr = textCursor();
94
95  // Get flag from last time and reset it before any possible returns
96  bool lastKeyWasReturn = myLastKeyWasReturn;
97  myLastKeyWasReturn = false;
98
99  if (isShift && event->key() == Qt::Key_Insert)
100    return paste();
101
102  if (isShift && event->key() == Qt::Key_Delete)
103    return cut();
104
105  if (isControl && event->key() == Qt::Key_Insert)
106    return copy();
107
108  if (isControl)
109  {
110    switch (event->key())
111    {
112      case Qt::Key_W:
113        cr.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor);
114        cr.removeSelectedText();
115        break;
116      case Qt::Key_U:
117        cr.select(QTextCursor::BlockUnderCursor);
118        if (!cr.hasSelection())
119          cr.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
120        if (!cr.hasSelection())
121          cr.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
122        cr.removeSelectedText();
123        break;
124      case Qt::Key_L:
125        cr.select(QTextCursor::Document);
126        cr.removeSelectedText();
127        break;
128      case Qt::Key_N: // Just the opposite of Ctrl+K
129        cr.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
130        if (!cr.hasSelection())
131          cr.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
132        cr.removeSelectedText();
133        break;
134      case Qt::Key_Return:
135      case Qt::Key_Enter:
136        if (Config::General::instance()->useDoubleReturn())
137          insertPlainText(QString("\n"));
138        else
139          emit ctrlEnterPressed();
140        break;
141      default:
142        MLEDIT_BASE::keyPressEvent(event);
143        break;
144    }
145    return;
146  }
147
148  if ((event->modifiers() & Qt::KeyboardModifierMask) == 0)
149  {
150    switch (event->key())
151    {
152      case Qt::Key_Return:
153      case Qt::Key_Enter:
154        if (lastKeyWasReturn && Config::General::instance()->useDoubleReturn())
155        {
156          // Return pressed twice, remove the previous line break and emit signal
157          cr.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
158          cr.removeSelectedText();
159          emit ctrlEnterPressed();
160          return;
161        }
162        else
163        {
164          // Return pressed once
165          myLastKeyWasReturn = true;
166        }
167        break;
168      case Qt::Key_Insert:
169        if (overwriteMode())
170        {
171          setOverwriteMode(false);
172          setCursorWidth(1);
173        }
174        else
175        {
176          setOverwriteMode(true);
177          setCursorWidth(2);
178        }
179        break;
180    }
181  }
182
183  MLEDIT_BASE::keyPressEvent(event);
184}
185
186void MLEdit::mousePressEvent(QMouseEvent* event)
187{
188  emit clicked();
189  MLEDIT_BASE::mousePressEvent(event);
190}
191
192#ifndef USE_KDE
193void MLEdit::contextMenuEvent(QContextMenuEvent* event)
194{
195  QMenu* menu=createStandardContextMenu();
196
197  if (!isReadOnly())
198  {
199    QAction* tabul = new QAction(tr("Allow Tabulations"), menu);
200    tabul->setCheckable(true);
201    tabul->setChecked(!tabChangesFocus());
202    connect(tabul, SIGNAL(triggered()), SLOT(toggleAllowTab()));
203    menu->addAction(tabul);
204  }
205
206  menu->exec(event->globalPos());
207  delete menu;
208}
209#endif
210
211void MLEdit::updateFont()
212{
213  setFont(myUseFixedFont ? Config::General::instance()->fixedFont() :
214      Config::General::instance()->editFont());
215
216  // Get height of current font
217  myFontHeight = fontMetrics().height();
218
219  // Set minimum height of text area to one line of text.
220  setMinimumHeight(heightForLines(1));
221}
222
223int MLEdit::heightForLines(int lines) const
224{
225  // We need to add frame width and the added height of the scroll area as
226  // we're calculating height for the widget, not the viewport.
227  return lines*myFontHeight + height() - viewport()->height() + 2 * frameWidth();
228}
229
230void MLEdit::setSizeHintLines(int lines)
231{
232  myLinesHint = lines;
233}
234
235QSize MLEdit::sizeHint() const
236{
237  QSize s = MLEDIT_BASE::sizeHint();
238  if (myLinesHint > 0)
239    s.setHeight(heightForLines(myLinesHint));
240  return s;
241}
242
243void MLEdit::toggleAllowTab()
244{
245  setTabChangesFocus(!tabChangesFocus());
246}
247
248#if 0
249//TODO: This may or may not be needed for KTextEdit in KDE 4
250
251#ifdef MLEDIT_USE_KTEXTEDIT
252/**
253 * @return the number of characters @a c at the end of @a str.
254 */
255static unsigned int countCharRev(const QString& str, const QChar c)
256{
257  unsigned int count = 0;
258  for (int pos = str.length() - 1; pos >= 0; pos--)
259  {
260    if (str.at(pos) != c)
261      break;
262    count += 1;
263  }
264  return count;
265}
266#endif
267
268/*
269 * KTextEdit adds a menu entry for doing spell checking. Unfortunatly KSpell
270 * (which is what KTextEdit uses to do the spell check) messes with the newlines
271 * at the end of the text it checks. That's why we need the hack below. It uses
272 * the fact that setText(const QString&) is non-virtual and only calls the
273 * virtual setText(const QString&, const QString&) with a null context.
274 *
275 * When KTextEdit calls setText(correctedText) after the spell check is done
276 * it will call QTextEdit::setText (since it's non-virtual). QTextEdit will
277 * then call setText(correctedText, QString::null) which will end up in the
278 * setText below (since it's virtual). And with m_fixSetTextNewlines set to
279 * true we can fix so that there is as many newlines at the end of the corrected
280 * text as there is in the old.
281 *
282 * On the other hand, when any class that uses MLEditWrap calls
283 * myMLEditWrapInstance->setText(myText) the call will end up at
284 * MLEditWrap::setText(myText) which will set m_fixSetTextNewlines to false before
285 * calling QTextEdit::setText(myText).
286 */
287void MLEditWrap::setText(const QString& text)
288{
289  m_fixSetTextNewlines = false;
290  MLEditWrapBase::setText(text);
291}
292
293void MLEditWrap::setText(const QString& txt, const QString& context)
294{
295  const bool modified = isModified(); // don't let setText reset this flag
296#ifdef MLEDIT_USE_KTEXTEDIT
297  const QString current = text();
298  if (m_fixSetTextNewlines && context.isNull())
299  {
300    const unsigned int currentNL = countCharRev(current, '\n');
301    const unsigned int txtNL = countCharRev(txt, '\n');
302    if (currentNL > txtNL)
303      MLEditWrapBase::setText(txt + QString().fill('\n', currentNL - txtNL), context);
304    else if (txtNL > currentNL)
305      MLEditWrapBase::setText(txt.left(txt.length() - (txtNL - currentNL)), context);
306    else
307      MLEditWrapBase::setText(txt, context);
308  }
309  else
310#endif
311    MLEditWrapBase::setText(txt, context);
312
313  setModified(modified);
314  m_fixSetTextNewlines = true;
315}
316#endif
Note: See TracBrowser for help on using the browser.