1 module examplewidgets.tabthread;
2 
3 import qt.config;
4 import qt.core.object;
5 import qt.core.string;
6 import qt.core.thread;
7 import qt.helpers;
8 import qt.widgets.ui;
9 import qt.widgets.widget;
10 
11 class Thread : QThread
12 {
13     mixin(Q_OBJECT_D);
14 
15 public:
16     /+ explicit +/this(QObject parent = null)
17     {
18         super(parent);
19     }
20 
21 /+ signals +/public:
22     @QSignal final void progressChanged(int value){mixin(Q_SIGNAL_IMPL_D);}
23 
24     @QSignal final void message(ref const(QString) msg){mixin(Q_SIGNAL_IMPL_D);}
25 
26 protected:
27     override extern(C++) void run()
28     {
29         QString tmp = "Started";
30         /+ emit +/ message(tmp);
31         for(int i = 1; i <= 100; i++)
32         {
33             if(isInterruptionRequested())
34             {
35                 QString tmp2 = "Canceled";
36                 /+ emit +/ message(tmp2);
37                 return;
38             }
39             QThread.msleep(500);
40             if (i % 10 == 0)
41             {
42                 QString tmp3 = QString.number(i) ~ "% done";
43                 /+ emit +/ message(tmp3);
44             }
45             /+ emit +/ progressChanged(i);
46         }
47         QString tmp4 = "Finished";
48         /+ emit +/ message(tmp4);
49     }
50 }
51 
52 class TabThread : QWidget
53 {
54     mixin(Q_OBJECT_D);
55 
56 public:
57     /+ explicit +/this(QWidget parent = null)
58     {
59         import core.stdcpp.new_;
60         super(parent);
61         this.ui = cpp_new!(typeof(*ui));
62 
63         ui.setupUi(this);
64     }
65     ~this()
66     {
67         import core.stdcpp.new_;
68 
69         if(thread)
70         {
71             thread.requestInterruption();
72             thread.wait();
73         }
74         cpp_delete(ui);
75     }
76 
77 private /+ slots +/:
78     @QSlot final void on_pushButtonStart_clicked()
79     {
80         import core.stdcpp.new_;
81 
82         ui.progressBar.setValue(0);
83         QString tmp = "";
84         ui.plainTextEdit.setPlainText(tmp);
85         thread = cpp_new!Thread(this);
86         connect(thread.signal!"progressChanged", ui.progressBar.slot!"setValue");
87         connect(thread.signal!"message", this.slot!"onThreadMessage");
88         connect(thread.signal!"finished", this.slot!"onThreadFinished");
89         connect(thread.signal!"finished", thread.slot!"deleteLater");
90         thread.start();
91         ui.pushButtonStart.setEnabled(false);
92         ui.pushButtonCancel.setEnabled(true);
93     }
94 
95     @QSlot final void on_pushButtonCancel_clicked()
96     {
97         thread.requestInterruption();
98     }
99 
100     @QSlot final void onThreadMessage(ref const(QString) msg)
101     {
102         ui.plainTextEdit.appendPlainText(msg);
103     }
104 
105     @QSlot final void onThreadFinished()
106     {
107         thread = null;
108         ui.pushButtonStart.setEnabled(true);
109         ui.pushButtonCancel.setEnabled(false);
110     }
111 
112 private:
113     UIStruct!"tabthread.ui"* ui;
114     Thread thread = null;
115 }
116