clock_t start = clock();
clock_t diff;
while(count-- > 0 && (diff = clock() - start) < CLOCKS_PER_SEC * 7 / 8){
+ int itemCount;
m_mutexGuiQueue.lock();
ReGuiQueueItem item = m_guiQueue.popFront();
+ itemCount = 1 + m_guiQueue.countOfType(item.m_type);
m_mutexGuiQueue.unlock();
- if (item.m_type == ReGuiQueueItem::ListEnd
- && (list = reinterpret_cast<QListWidget*>(item.m_widget))->count() >= m_maxListSize)
- delete list->takeItem(0);
+ if (item.m_type == ReGuiQueueItem::ListEnd){
+ if (itemCount <= m_maxListSize){
+ if ( (list = reinterpret_cast<QListWidget*>(item.m_widget))->count() >= m_maxListSize)
+ delete list->takeItem(0);
+ item.apply(m_guiQueue, m_maxListSize);
+ item.m_type = ReGuiQueueItem::Undef;
+ }
+ }
if (item.m_type == ReGuiQueueItem::Undef)
break;
- if (! item.apply()){
+ int maxItems = 0;
+ switch(item.m_type){
+ case ReGuiQueueItem::ListEnd:
+ case ReGuiQueueItem::ListAppendToCurrent:
+ maxItems = m_maxListSize * 2;
+ break;
+ default:
+ break;
+ }
+ if (item.m_type != ReGuiQueueItem::Undef
+ && ! item.apply(m_guiQueue, maxItems)){
switch (item.m_type){
case ReGuiQueueItem::ListEnd:
ui->listWidgetLog->addItem(item.m_value);
}
}
}
+ ReGuiQueueItem& item = m_guiQueue.lastOfType(ReGuiQueueItem::ListEnd);
+ if (item.m_type == ReGuiQueueItem::ListEnd){
+ if ( (list = reinterpret_cast<QListWidget*>(item.m_widget))->count() >= m_maxListSize)
+ delete list->takeItem(0);
+ item.apply(m_guiQueue, 0);
+ item.m_type = ReGuiQueueItem::Undef;
+ }
if (!statusMessage.isEmpty())
setStatusMessage(LOG_INFO, statusMessage);
* Constructor.
*/
ReGuiQueue::ReGuiQueue() :
- QList<ReGuiQueueItem>(),
- m_locker()
+ QList<ReGuiQueueItem>(),
+ m_locker(),
+ m_countOfType(),
+ m_lastOfType()
{
+ memset(&m_countOfType, 0, sizeof m_countOfType);
}
/**
{
m_locker.lock();
append(item);
+ ++m_countOfType[item.m_type];
m_locker.unlock();
}
{
m_locker.lock();
ReGuiQueueItem rc = takeFirst();
+ m_lastOfType[rc.m_type] = rc;
+ --m_countOfType[rc.m_type];
m_locker.unlock();
return rc;
}
*
* This method should only used by the master thread.
*
+ * @param
+ * @param maxItems 0 or the maximal count of relevant items: items will be
+ * inserted only if the count of items in the queue
+ * is lower than this limit. This is a widget type specific
+ * value
* @return <code>true</code>: the info could be put into the widget<br>
* <code>false</code>: nothing is done
*/
-bool ReGuiQueueItem::apply() const
+bool ReGuiQueueItem::apply(ReGuiQueue &queue, int maxItems) const
{
bool rc = m_widget != NULL;
if (rc){
- switch(m_type){
- case LabelText:
- reinterpret_cast<QLabel*>(m_widget)->setText(m_value);
- break;
- case NewTableRow:
- {
- QChar separator = m_value.at(0);
- QStringList list = m_value.mid(1).split(separator);
- QTableWidget* table = reinterpret_cast<QTableWidget*>(m_widget);
- int rowCount = table->rowCount();
- table->setRowCount(rowCount + 1);
- int cols = min(list.size(), table->columnCount());
- for (int ix = 0; ix < cols; ix++){
- table->setItem(rowCount, ix, new QTableWidgetItem(list.at(ix)));
+ if (maxItems > 0 && queue.m_countOfType[m_type] > maxItems){
+ // nothing to do!
+ } else {
+ switch(m_type){
+ case LabelText:
+ reinterpret_cast<QLabel*>(m_widget)->setText(m_value);
+ break;
+ case NewTableRow:
+ {
+ QChar separator = m_value.at(0);
+ QStringList list = m_value.mid(1).split(separator);
+ QTableWidget* table = reinterpret_cast<QTableWidget*>(m_widget);
+ int rowCount = table->rowCount();
+ table->setRowCount(rowCount + 1);
+ int cols = min(list.size(), table->columnCount());
+ for (int ix = 0; ix < cols; ix++){
+ table->setItem(rowCount, ix, new QTableWidgetItem(list.at(ix)));
+ }
+ break;
+ }
+ case ListEnd:
+ {
+ QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
+ list->addItem(m_value);
+ int count = list->count();
+ list->setCurrentRow(count - 1);
+ break;
+ }
+ case ListAppendToCurrent:
+ {
+ QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
+ int count = list->count();
+ if (count > 0){
+ QListWidgetItem* item = list->item(count - 1);
+ item->setText(item->text() + " " + m_value);
+ }
+ break;
+ }
+ case ListReplaceCurrent:
+ {
+ QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
+ int count = list->count();
+ QListWidgetItem* item = list->item(count - 1);
+ item->setText(m_value);
+ break;
+ }
+ default:
+ rc = false;
+ break;
}
- break;
- }
- case ListEnd:
- {
- QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
- list->addItem(m_value);
- int count = list->count();
- list->setCurrentRow(count - 1);
- break;
- }
- case ListAppendToCurrent:
- {
- QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
- int count = list->count();
- QListWidgetItem* item = list->item(count - 1);
- item->setText(item->text() + " " + m_value);
- break;
- }
- case ListReplaceCurrent:
- {
- QListWidget* list = reinterpret_cast<QListWidget*>(m_widget);
- int count = list->count();
- QListWidgetItem* item = list->item(count - 1);
- item->setText(m_value);
- break;
- }
- default:
- rc = false;
- break;
}
}
return rc;
#ifndef REGUIQUEUE_HPP
#define REGUIQUEUE_HPP
+class ReGuiQueue;
class ReGuiQueueItem {
public:
enum WidgetType {
Undef, LabelText, NewTableRow, ListEnd, ListAppendToCurrent,
ListReplaceCurrent,
LogMessage, LogError, ReadyMessage, StatusLine,
- UserDefined1, UserDefined2
+ UserDefined1, UserDefined2,
+ // must be the last!
+ TypeCount
};
public:
return *this;
}
public:
- bool apply() const;
+ bool apply(ReGuiQueue &queue, int maxItems = 0) const;
public:
WidgetType m_type;
QWidget* m_widget;
int count() const;
ReGuiQueueItem popFront();
void pushBack(const ReGuiQueueItem& item);
+ inline int countOfType(ReGuiQueueItem::WidgetType type) const {
+ return m_countOfType[type];
+ }
+ inline ReGuiQueueItem& lastOfType(ReGuiQueueItem::WidgetType type){
+ return m_lastOfType[type];
+ }
+
protected:
QMutex m_locker;
+ friend class ReGuiQueueItem;
+ // count of items for each widget type:
+ int m_countOfType[ReGuiQueueItem::TypeCount];
+ // the last item of each widget type which is removed from the queue:
+ ReGuiQueueItem m_lastOfType[ReGuiQueueItem::TypeCount];
};
#endif // REGUIQUEUE_HPP