ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rlistwidget.cpp
Go to the documentation of this file.
1 #include <QUuid>
2 #include "rlistwidget.h"
3 
5  QListWidget(parent)
6 {
7  setDragDropMode(QAbstractItemView::InternalMove);
8  setMovement(QListView::Snap);
9 
10  reorderTimer.setSingleShot(true);
11  reorderTimer.setInterval(100);
12  connect(&reorderTimer, SIGNAL(timeout()), this, SLOT(timerTick()));
13 
14  connect(this, SIGNAL(itemEntered(QListWidgetItem*)), this, SLOT(enter(QListWidgetItem*)));
15  connect(this, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(change(QListWidgetItem*)));
16 }
17 
19  bool reordered = false;
20  QListWidgetItem* reselect = 0;
21  QList<QVariant> newItems;
22  // Scan the item list to see if they changed
23  int c = count();
24  for(int i = 0; i < c; i++){
25  QListWidgetItem* itm = item(i);
26  QVariant data = itm->data(DATA_ROLE);
27  newItems.append(data);
28  if(i >= previousItems.count() || data != previousItems[i])
29  reordered = true;
30  // Re-select the dragged item (if any)
31  if(data == dragged)
32  reselect = itm;
33  }
34  if(previousItems.length() != newItems.length())
35  return;
36  if(reordered){
37  previousItems = newItems;
38  emit orderChanged();
39  if(reselect){
40  reselect->setSelected(true);
41  setCurrentItem(reselect);
42  dragged = QVariant();
43  }
44  }
45 }
46 
47 void RListWidget::enter(QListWidgetItem* item){
48  rescanItems();
49  // Check for drag+drop setup
50  if(item)
51  dragged = item->data(DATA_ROLE);
52 }
53 
54 void RListWidget::change(QListWidgetItem* item){
55  reorderTimer.stop();
56  reorderTimer.start();
57 }
58 
60  QList<QVariant> newItems;
61  int c = count();
62  for(int i = 0; i < c; i++){
63  QVariant data = this->item(i)->data(DATA_ROLE);
64  if(data.isNull()){
65  // Generate the ID for this item if it doesn't already exist
66  data = QUuid::createUuid();
67  this->item(i)->setData(DATA_ROLE, data);
68  }
69  newItems.append(data);
70  }
71  previousItems = newItems;
72 }
void timerTick()
Definition: rlistwidget.cpp:18
QTimer reorderTimer
Definition: rlistwidget.h:27
void change(QListWidgetItem *item)
Definition: rlistwidget.cpp:54
void enter(QListWidgetItem *item)
Definition: rlistwidget.cpp:47
QVariant dragged
Definition: rlistwidget.h:26
RListWidget(QWidget *parent=0)
Definition: rlistwidget.cpp:4
QList< QVariant > previousItems
Definition: rlistwidget.h:25
void rescanItems()
Definition: rlistwidget.cpp:59
static const int DATA_ROLE
Definition: rlistwidget.h:29