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