summaryrefslogtreecommitdiffstats
blob: 7ce5d88e0dd55f651a12166e3d42abc00516f22f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <QPointer>
#include <QTextEdit>

#include <Swift/Controllers/Settings/SettingsProvider.h>

#include <SwifTools/SpellParser.h>

#include <Swift/QtUI/QtSpellCheckHighlighter.h>

namespace Swift {
    class SpellChecker;
    class QtSpellCheckerWindow;

    class QtTextEdit : public QTextEdit {
        Q_OBJECT
    public:
        QtTextEdit(SettingsProvider* settings, QWidget* parent = nullptr);
        virtual ~QtTextEdit();
        virtual QSize sizeHint() const;

        void setEmphasiseFocus(bool emphasise);
        void setCorrectionHighlight(bool coorectionHighlight);

    signals:
        void wordCorrected(QString& word);
        void returnPressed();
        void unhandledKeyPressEvent(QKeyEvent* event);
        void receivedFocus();
        void lostFocus();
        void itemDropped(QDropEvent* event);

    public slots:
        void handleSettingChanged(const std::string& settings);

    protected:
        virtual void keyPressEvent(QKeyEvent* event);
        virtual void focusInEvent(QFocusEvent* event);
        virtual void focusOutEvent(QFocusEvent* event);
        virtual void contextMenuEvent(QContextMenuEvent* event);
        virtual void dropEvent(QDropEvent* event);

    private slots:
        void handleTextChanged();

    private:
        void addSuggestions(QMenu* menu, QContextMenuEvent* event);
        void replaceMisspelledWord(const QString& word, int cursorPosition);
        void setUpSpellChecker();
        void spellCheckerSettingsWindow();
        PositionPair getWordFromCursor(int cursorPosition);
        void updateStyleSheet();

    private:
        SpellChecker* checker_;
        QtSpellCheckHighlighter* highlighter_;
        std::vector<QAction*> replaceWordActions_;
        SettingsProvider* settings_;
        QPointer<QtSpellCheckerWindow> spellCheckerWindow_;
        bool emphasiseFocus_ = false;
        bool correctionHighlight_ = false;
    };
}