OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / proxy / textwidget.h
1 #ifndef TEXTWIDGET_H
2 #define TEXTWIDGET_H
3
4 #include "userdata.h"
5 #include "text.h"
6
7 #include <QWidget>
8 #include <QScrollArea>
9 #include <QPlainTextEdit>
10
11 #include <QVariant>
12 #include <QSize>
13 #include <QPoint>
14
15 #include <QList>
16
17 #include <memory>
18
19 QT_BEGIN_NAMESPACE
20 class QFocusEvent;
21 class QHideEvent;
22 class QInputMethodEvent;
23 class QKeyEvent;
24 class QMouseEvent;
25 class QResizeEvent;
26 class QShowEvent;
27 QT_END_NAMESPACE
28
29 class TextArea;
30 class TextWidget;
31
32 /*!
33  * \brief コンテンツに応じて大きさを変えるテキスト編集パネルです。
34  */
35 class TextPanel : public QPlainTextEdit
36 {
37         friend class TextArea;
38         Q_OBJECT
39 public:
40         explicit TextPanel(QWidget *parent);
41
42         void set_string(QString const &string_);
43
44         TextArea* parent_text_area();
45         QSize contents_size() const;
46
47         template <typename T>
48         T* user_data() { return static_cast<T*>(m_user_data.get()); }
49         void set_user_data(UserData::pointer data);
50
51         QSize sizeHint() const;
52
53 signals:
54         void focusIn();
55         void focusOut();
56
57 protected:
58         void focusInEvent(QFocusEvent *ev);
59         void focusOutEvent(QFocusEvent *ev);
60         void hideEvent(QHideEvent *ev);
61         void inputMethodEvent(QInputMethodEvent *ev);
62         QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
63         void keyPressEvent(QKeyEvent *ev);
64         void mousePressEvent(QMouseEvent *ev);
65         void mouseReleaseEvent(QMouseEvent *ev);
66         void showEvent(QShowEvent *ev);
67
68         virtual void do_click(QPoint const &pos);
69         virtual void do_focusin();
70         virtual void do_focusout();
71
72 private:
73         int m_contents_offset;
74         UserData::pointer m_user_data;
75         Text::pointer m_text;
76
77         QPoint m_mouse_press_pos;
78 };
79
80 /*!
81  * \brief TextPanelをリスト状に束ねたウィジェットです。
82  */
83 class TextArea : public QWidget
84 {
85         friend class TextPanel;
86         Q_OBJECT
87
88 public:
89         typedef QList<TextPanel*>::iterator iterator;
90
91 public:
92         explicit TextArea(QWidget *parent = 0);
93
94         TextWidget* parent_text_widget();
95
96         void clear();
97         TextPanel* append(TextPanel *panel);
98         template <typename T>
99         T* append() { return static_cast<T*>(append(new T(this))); }
100
101         //TextPanel* current_panel();
102         TextPanel* find_previous_panel(TextPanel *panel);
103         TextPanel* find_next_panel(TextPanel *panel);
104         TextPanel* find_previous_visible_panel(TextPanel *panel);
105         TextPanel* find_next_visible_panel(TextPanel *panel);
106
107         iterator begin();
108         iterator end();
109
110         void focus_previous(TextPanel *panel);
111         void focus_next(TextPanel *panel);
112
113 signals:
114         void focusInChild(TextPanel *new_, TextPanel *old_);
115
116 protected:
117         void resizeEvent(QResizeEvent *ev);
118
119         void ensure_visible(TextPanel *panel);
120         void perform_resize();
121
122         virtual void do_focusin_child(TextPanel *panel);
123         virtual void do_focusout_child(TextPanel *panel);
124
125 protected:
126         QList<TextPanel*> m_panels;
127         TextPanel *m_current_panel;
128 };
129
130 /*!
131  * \brief TextAreaにスクロールバーをつけたウィジェットです。
132  *
133  * スクロールバー以外の機能を持たないため、ほとんどの処理はTextAreaに対して
134  * 行ってください。
135  */
136 class TextWidget : public QScrollArea
137 {
138         friend class TextArea;
139         Q_OBJECT
140 public:
141         explicit TextWidget(QWidget *parent = 0);
142
143         TextArea* text_area();
144
145 private:
146         TextArea *m_twidget;
147 };
148
149 #endif // TEXTWIDGET_H