1 module examplewidgets.tabtext;
2 
3 import qt.config;
4 import qt.helpers;
5 import qt.widgets.ui;
6 import qt.widgets.widget;
7 
8 class TabText : QWidget
9 {
10     mixin(Q_OBJECT_D);
11 
12 public:
13     /+ explicit +/this(QWidget parent = null)
14     {
15         import core.stdcpp.new_;
16         super(parent);
17         this.ui = cpp_new!(typeof(*ui));
18 
19         ui.setupUi(this);
20 
21         connect(ui.textEdit.signal!"undoAvailable", ui.pushButtonUndo.slot!"setEnabled");
22         connect(ui.textEdit.signal!"redoAvailable", ui.pushButtonRedo.slot!"setEnabled");
23         connect(ui.pushButtonUndo.signal!"clicked", ui.textEdit.slot!"undo");
24         connect(ui.pushButtonRedo.signal!"clicked", ui.textEdit.slot!"redo");
25 
26         connect(ui.pushButtonBold.signal!"clicked", this.slot!"changeFormat");
27         connect(ui.pushButtonItalic.signal!"clicked", this.slot!"changeFormat");
28 
29         on_textEdit_textChanged();
30     }
31     ~this()
32     {
33         import core.stdcpp.new_;
34 
35         cpp_delete(ui);
36     }
37 
38 private /+ slots +/:
39     @QSlot final void on_textEdit_textChanged()
40     {
41         ui.textBrowser.setHtml(ui.textEdit.document().toHtml());
42     }
43 
44     @QSlot final void on_textEdit_cursorPositionChanged()
45     {
46         import qt.gui.font;
47         import qt.gui.textcursor;
48 
49         QTextCursor cursor = ui.textEdit.textCursor();
50         ui.pushButtonList.setChecked(cursor.currentList() !is null);
51         ui.pushButtonBold.setChecked(cursor.charFormat().fontWeight() == QFont.Weight.Bold);
52         ui.pushButtonItalic.setChecked(cursor.charFormat().fontItalic());
53     }
54 
55     @QSlot final void changeFormat()
56     {
57         import qt.gui.font;
58         import qt.gui.textcursor;
59         import qt.gui.textformat;
60 
61         QTextCharFormat format;
62         format.setFontWeight(ui.pushButtonBold.isChecked() ? QFont.Weight.Bold : QFont.Weight.Normal);
63         format.setFontItalic(ui.pushButtonItalic.isChecked());
64         QTextCursor cursor = ui.textEdit.textCursor();
65         cursor.mergeCharFormat(format);
66         ui.textEdit.mergeCurrentCharFormat(format);
67         ui.textEdit.setFocus();
68     }
69 
70     @QSlot final void on_pushButtonList_clicked()
71     {
72         import qt.gui.textcursor;
73         import qt.gui.textformat;
74         import qt.gui.textlist;
75 
76         QTextCursor cursor = ui.textEdit.textCursor();
77         if(ui.pushButtonList.isChecked())
78         {
79             QTextListFormat listFmt;
80             listFmt.setStyle(QTextListFormat.Style.ListDecimal);
81             //cursor.createList(listFmt);TODO: only the following works
82             cursor.createList(QTextListFormat.Style.ListDecimal);
83         }
84         else
85         {
86             QTextList list = cursor.currentList();
87             if(list)
88             {
89                 auto tmp = cursor.block(); list.remove(tmp);
90             }
91 
92             QTextBlockFormat blockFmt = cursor.blockFormat();
93             blockFmt.setIndent(0);
94             cursor.setBlockFormat(blockFmt);
95         }
96     }
97 
98 private:
99     UIStruct!"tabtext.ui"* ui;
100 }
101