1 /*
2    Strawberry Music Player
3    This file was part of Clementine.
4    Copyright 2010, David Sansome <me@davidsansome.com>
5 
6    Strawberry 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 3 of the License, or
9    (at your option) any later version.
10 
11    Strawberry 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 Strawberry.  If not, see <http://www.gnu.org/licenses/>.
18 
19  */
20 
21 #include "config.h"
22 
23 #include <QtGlobal>
24 #include <QWidget>
25 #include <QSize>
26 #include <QTextEdit>
27 #include <QTextOption>
28 #include <QFontMetrics>
29 #include <QSizePolicy>
30 #include <QtEvents>
31 
32 #include "linetextedit.h"
33 
LineTextEdit(QWidget * parent)34 LineTextEdit::LineTextEdit(QWidget *parent) : QTextEdit(parent) {
35 
36   setWordWrapMode(QTextOption::NoWrap);
37   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
38   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39   setTabChangesFocus(true);
40   setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
41 
42 }
43 
sizeHint() const44 QSize LineTextEdit::sizeHint() const {
45   QFontMetrics fm(font());
46 
47   static const int kMargin = 5;
48   int h = 2 * kMargin + qMax(fm.height(), 14);
49 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
50   int w = 2 * kMargin + fm.horizontalAdvance("W") * 15;
51 #else
52   int w = 2 * kMargin + fm.width("W") * 15;
53 #endif
54 
55   return QSize(w, h);
56 }
57 
minimumSizeHint() const58 QSize LineTextEdit::minimumSizeHint() const {
59   return sizeHint();
60 }
61 
keyPressEvent(QKeyEvent * e)62 void LineTextEdit::keyPressEvent(QKeyEvent *e) {
63   if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
64     e->ignore();
65   }
66   else {
67     QTextEdit::keyPressEvent(e);
68   }
69 }
70