qavrg 0.0.28
|
00001 #include "qavrgtestwindow.h" 00002 00003 #include <QHBoxLayout> 00004 #include <stdio.h> 00005 00006 QavrgTestProxy::QavrgTestProxy(int useProxy, int useTransaction, QavrgTestInteger *integer, QSpinBox *spinBox, QObject *parent) 00007 : QObject(parent), 00008 m_UseProxy(useProxy), 00009 m_UseTransaction(useTransaction), 00010 m_Value(integer->get_Integer()), 00011 m_Integer(integer), 00012 m_SpinBox(spinBox), 00013 m_TransactionId(0) 00014 { 00015 } 00016 00017 void QavrgTestProxy::valueChanged(int value, qint64 id) 00018 { 00019 printf("QavrgTestProxy::valueChanged(%d,%ld)\n", value, (long int) id); 00020 00021 if (value != m_Value) { 00022 m_Value = value; 00023 00024 if (m_UseTransaction) { 00025 if (id > m_TransactionId) { 00026 m_TransactionId = id; 00027 00028 printf("emit changeSpinBox(%d)\n", m_Value); 00029 00030 emit changeSpinBox(value); 00031 } else { 00032 printf("emission skipped because %ld <= %ld\n", (long int) id, (long int) m_TransactionId); 00033 } 00034 } else { 00035 emit changeSpinBox(value); 00036 } 00037 } 00038 } 00039 00040 void QavrgTestProxy::spinBoxChanged(int value) 00041 { 00042 printf("QavrgTestProxy::spinBoxChanged(%d)\n", value); 00043 00044 if (value != m_Value) { 00045 m_Value = value; 00046 00047 printf("emit changeInteger(%d, %ld)\n", m_Value, (long int) m_TransactionId); 00048 00049 emit changeInteger(m_Value, m_TransactionId++); 00050 } 00051 } 00052 00053 QavrgTestInteger::QavrgTestInteger(int useProxy, int useTransaction, QObject *parent) 00054 : QObject(parent), 00055 m_Mutex(QMutex::Recursive), 00056 m_UseProxy(useProxy), 00057 m_UseTransaction(useTransaction), 00058 m_Value(0), 00059 m_TransactionId(0), 00060 m_NQueuedUpdates(0) 00061 { 00062 } 00063 00064 void QavrgTestInteger::linkTo(QSpinBox *spinBox) 00065 { 00066 if (m_UseProxy) { 00067 QavrgTestProxy *proxy = new QavrgTestProxy(m_UseProxy, m_UseTransaction, this, spinBox, NULL); 00068 00069 proxy -> moveToThread(spinBox -> thread()); 00070 00071 connect(this, SIGNAL(chg_Integer(int,qint64)), proxy, SLOT(valueChanged(int,qint64)), Qt::QueuedConnection); 00072 connect(proxy, SIGNAL(changeInteger(int,qint64)), this, SLOT(setValue(int,qint64)), Qt::QueuedConnection); 00073 00074 connect(spinBox, SIGNAL(valueChanged(int)), proxy, SLOT(spinBoxChanged(int)), Qt::DirectConnection); 00075 connect(proxy, SIGNAL(changeSpinBox(int)), spinBox, SLOT(setValue(int)), Qt::DirectConnection); 00076 00077 spinBox -> setValue(get_Integer()); 00078 } else { 00079 connect(this, SIGNAL(chg_Integer(int,qint64)), spinBox, SLOT(setValue(int)), Qt::DirectConnection); 00080 connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)), Qt::DirectConnection); 00081 } 00082 } 00083 00084 void QavrgTestInteger::setValue(int value, qint64 id) 00085 { 00086 printf("QavrgTestInteger::setValue(%d,%ld,%d)\n", value, (long int) id, m_NQueuedUpdates); 00087 00088 m_NQueuedUpdates--; 00089 00090 set_Integer(value); 00091 } 00092 00093 void QavrgTestInteger::set_Integer(int value) 00094 { 00095 QMutexLocker lock(&m_Mutex); 00096 00097 printf("%ld: QavrgTestInteger::set_Integer(%d)\n", (long int) m_TransactionId, value); 00098 00099 if (value != m_Value) { 00100 m_Value = value; 00101 00102 m_NQueuedUpdates++; 00103 00104 emit chg_Integer(m_Value, m_TransactionId++); 00105 } 00106 } 00107 00108 int QavrgTestInteger::get_Integer() const 00109 { 00110 QMutexLocker lock(&m_Mutex); 00111 00112 return m_Value; 00113 } 00114 00115 QavrgTestThread::QavrgTestThread(int useProxy, int useTransaction, QavrgTestWindow *win, QObject *parent) 00116 : QThread(parent), 00117 m_Integer(NULL), 00118 m_UseProxy(useProxy), 00119 m_UseTransaction(useTransaction), 00120 m_Window(win) 00121 { 00122 } 00123 00124 void QavrgTestThread::run() 00125 { 00126 m_Integer = new QavrgTestInteger(m_UseProxy, m_UseTransaction); 00127 00128 m_Integer -> linkTo(m_Window -> m_SpinBox); 00129 00130 exec(); 00131 } 00132 00133 QavrgTestWindow::QavrgTestWindow(int useProxy, int useTransaction, QWidget *parent) 00134 : QWidget(parent), 00135 m_SpinBox(NULL), 00136 m_Increment(NULL), 00137 m_DoubleIncrement(NULL), 00138 m_CountFive(NULL), 00139 m_UseProxy(useProxy), 00140 m_UseTransaction(useTransaction), 00141 m_Thread(NULL) 00142 { 00143 m_Thread = new QavrgTestThread(m_UseProxy, m_UseTransaction, this, this); 00144 00145 QHBoxLayout *layout = new QHBoxLayout(); 00146 00147 m_SpinBox = new QSpinBox(); 00148 m_Increment = new QPushButton("I++"); 00149 m_DoubleIncrement = new QPushButton("I++;I++"); 00150 m_CountFive = new QPushButton("12345"); 00151 m_CountThousand = new QPushButton("Thousand"); 00152 00153 m_SpinBox -> setMinimum(-1000); 00154 m_SpinBox -> setMaximum(99999); 00155 00156 layout -> addWidget(m_SpinBox); 00157 layout -> addWidget(m_Increment); 00158 layout -> addWidget(m_DoubleIncrement); 00159 layout -> addWidget(m_CountFive); 00160 layout -> addWidget(m_CountThousand); 00161 00162 setLayout(layout); 00163 00164 connect(m_Increment, SIGNAL(clicked()), this, SLOT(increment())); 00165 connect(m_DoubleIncrement, SIGNAL(clicked()), this, SLOT(doubleIncrement())); 00166 connect(m_CountFive, SIGNAL(clicked()), this, SLOT(countFive())); 00167 connect(m_CountThousand, SIGNAL(clicked()), this, SLOT(countThousand())); 00168 00169 m_Thread -> start(); 00170 00171 show(); 00172 } 00173 00174 void QavrgTestWindow::increment() 00175 { 00176 int val = m_Thread -> m_Integer -> get_Integer(); 00177 00178 printf("QavrgTestWindow::increment -> %d\n", val); 00179 00180 m_Thread -> m_Integer -> set_Integer(val+1); 00181 // emit set_Integer(val+1); 00182 } 00183 00184 void QavrgTestWindow::doubleIncrement() 00185 { 00186 printf("QavrgTestWindow::doubleIncrement\n"); 00187 00188 increment(); 00189 increment(); 00190 } 00191 00192 void QavrgTestWindow::countFive() 00193 { 00194 printf("QavrgTestWindow::countFive"); 00195 00196 m_Thread -> m_Integer -> set_Integer(1); 00197 m_Thread -> m_Integer -> set_Integer(2); 00198 m_Thread -> m_Integer -> set_Integer(3); 00199 m_Thread -> m_Integer -> set_Integer(4); 00200 m_Thread -> m_Integer -> set_Integer(5); 00201 } 00202 00203 void QavrgTestWindow::countThousand() 00204 { 00205 for (int i=1; i<=1000; i++) { 00206 m_Thread -> m_Integer -> set_Integer(i); 00207 } 00208 }