modernize: Reformat ALL the source... again!
[quassel.git] / src / qtui / inputwidget.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.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) version 3.                                           *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <QHash>
24 #include <QIcon>
25 #include <QMap>
26 #include <QTextCharFormat>
27
28 #include "abstractitemview.h"
29 #include "action.h"
30 #include "bufferinfo.h"
31 #include "buffermodel.h"
32 #include "identity.h"
33 #include "network.h"
34
35 #include "ui_inputwidget.h"
36
37 class MultiLineEdit;
38
39 class InputWidget : public AbstractItemView
40 {
41     Q_OBJECT
42
43 public:
44     InputWidget(QWidget* parent = nullptr);
45
46     const Network* currentNetwork() const;
47
48     inline MultiLineEdit* inputLine() const { return ui.inputEdit; }
49
50 public slots:
51     /**
52      * Apply the active color to the selected or typed text
53      *
54      * Active color is chosen using the UI menu.
55      */
56     void applyFormatActiveColor();
57
58     /**
59      * Apply the active fill color to the selected or typed text background
60      *
61      * Fill color is chosen using the UI menu.
62      */
63     void applyFormatActiveColorFill();
64
65     /**
66      * Toggle the boldness of the selected or typed text
67      *
68      * Bold becomes normal, and normal becomes bold.
69      */
70     void toggleFormatBold();
71
72     /**
73      * Toggle the italicness of the selected or typed text
74      *
75      * Italicized becomes normal, and normal becomes italicized.
76      */
77     void toggleFormatItalic();
78
79     /**
80      * Toggle the underlining of the selected or typed text
81      *
82      * Underlined becomes normal, and normal becomes underlined.
83      */
84     void toggleFormatUnderline();
85
86     /**
87      * Clear the formatting of the selected or typed text
88      *
89      * Clears the font weight (bold, italic, underline) and foreground/background coloring.
90      */
91     void clearFormat();
92
93 protected:
94     bool eventFilter(QObject* watched, QEvent* event) override;
95
96 protected slots:
97     void currentChanged(const QModelIndex& current, const QModelIndex& previous) override;
98     void rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end) override;
99     void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) override;
100
101 private slots:
102     void setCustomFont(const QVariant& font);
103     void setUseCustomFont(const QVariant&);
104     void setEnableEmacsMode(const QVariant&);
105     void setShowNickSelector(const QVariant&);
106     void setShowStyleButtons(const QVariant&);
107     void setEnablePerChatHistory(const QVariant&);
108     void setMaxLines(const QVariant&);
109     void setLineWrapEnabled(const QVariant&);
110     void setMultiLineEnabled(const QVariant&);
111     void setScrollBarsEnabled(const QVariant&);
112     void onTextEntered(const QString& text);
113     void changeNick(const QString& newNick) const;
114
115     void setNetwork(NetworkId networkId);
116     void setIdentity(IdentityId identityId);
117     void connectMyIrcUser();
118     void updateNickSelector() const;
119     void updateEnabledState();
120
121     BufferInfo currentBufferInfo() const;
122
123     /**
124      * Set whether or not the style options frame is expanded
125      *
126      * @param visible If true, expand the style options frame, otherwise collapse it
127      */
128     void setStyleOptionsExpanded(const bool visible);
129
130     void currentCharFormatChanged(const QTextCharFormat& format);
131     void on_showStyleButton_toggled(bool checked);
132     void on_boldButton_clicked(bool checked);
133     void on_italicButton_clicked(bool checked);
134     void on_underlineButton_clicked(bool checked);
135     void colorChosen(QAction* action);
136     void colorHighlightChosen(QAction* action);
137
138 private:
139     /**
140      * Clear the formatting of the text, globally or selected text only
141      *
142      * Clears the font weight (bold, italic, underline) and foreground/background coloring.
143      *
144      * @param global If true, clear all text formatting, otherwise only clear selected text
145      */
146     void setFormatClear(const bool global = false);
147
148     /**
149      * Sets the boldness of the selected or typed text
150      *
151      * @param bold If true, set text bold, otherwise set text normal
152      */
153     void setFormatBold(const bool bold);
154
155     /**
156      * Sets the italicness of the selected or typed text
157      *
158      * @param bold If true, set text italic, otherwise set text normal
159      */
160     void setFormatItalic(const bool italic);
161
162     /**
163      * Sets the underline of the selected or typed text
164      *
165      * @param bold If true, set text underlined, otherwise set text normal
166      */
167     void setFormatUnderline(const bool underline);
168
169     Ui::InputWidget ui;
170
171     NetworkId _networkId;
172     IdentityId _identityId;
173     QMenu *_colorMenu, *_colorFillMenu;
174
175     void mergeFormatOnSelection(const QTextCharFormat& format);
176     void fontChanged(const QFont& f);
177     QIcon createColorToolButtonIcon(const QIcon& icon, const QColor& color);
178     QTextCharFormat getFormatOfWordOrSelection();
179     void setFormatOnSelection(const QTextCharFormat& format);
180
181     bool _perChatHistory;
182     struct HistoryState
183     {
184         QStringList history;
185         QHash<int, QString> tempHistory;
186         qint32 idx{0};
187         QString inputLine;
188     };
189
190     QMap<BufferId, HistoryState> historyMap;
191 };
192
193 class MouseWheelFilter : public QObject
194 {
195     Q_OBJECT
196
197 public:
198     MouseWheelFilter(QObject* parent = nullptr);
199     bool eventFilter(QObject* obj, QEvent* event) override;
200 };