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
kbanimwidget.cpp
Go to the documentation of this file.
1 #include <QDateTime>
2 #include <QMenu>
3 #include "animsettingdialog.h"
4 #include "kbanimwidget.h"
5 #include "ui_kbanimwidget.h"
6 
8  QWidget(parent), light(0), current(0), noReorder(false),
9  ui(new Ui::KbAnimWidget)
10 {
11  ui->setupUi(this);
12  ui->animList->setVisible(false);
13  setCurrent(0);
14  connect(ui->animList, SIGNAL(orderChanged()), this, SLOT(reorderAnims()));
15 }
16 
18  delete ui;
19 }
20 
22  if(light != newLight){
23  if(light)
24  disconnect(light, SIGNAL(didLoad()), this, SLOT(refreshList()));
25  if(newLight)
26  connect(newLight, SIGNAL(didLoad()), this, SLOT(refreshList()));
27  light = newLight;
28  }
29  refreshList();
30 }
31 
33  noReorder = true;
34  setCurrent(0);
35  ui->animList->clear();
36  animations.clear();
37  // Add the animations from the new lighting mode
38  if(!light){
39  ui->animList->setVisible(false);
40  ui->noAnimLabel->setVisible(true);
41  return;
42  }
43  QList<KbAnim*> newAnimations = light->animList();
44  if(newAnimations.count() == 0){
45  ui->animList->setVisible(false);
46  ui->noAnimLabel->setVisible(true);
47  return;
48  }
49  ui->animList->setVisible(true);
50  foreach(KbAnim* anim, newAnimations){
51  QListWidgetItem* item = new QListWidgetItem(anim->name(), ui->animList);
52  item->setData(Qt::UserRole, anim->guid());
53  item->setFlags(item->flags() | Qt::ItemIsEditable);
54  animations[anim->guid()] = anim;
55  ui->animList->addItem(item);
56  }
57  ui->noAnimLabel->setVisible(false);
58  noReorder = false;
59 }
60 
62  if(light && !noReorder){
63  // Clear and rebuild the list of animations in case the animation moved
64  int count = ui->animList->count();
65  QList<KbAnim*> animList;
66  for(int i = 0; i < count; i++){
67  QListWidgetItem* item = ui->animList->item(i);
68  KbAnim* anim = animations[item->data(Qt::UserRole).toUuid()];
69  if(anim && !animList.contains(anim))
70  animList.append(anim);
71  item->setFlags(item->flags() | Qt::ItemIsEditable);
72  }
73  light->animList(animList);
74  }
75 }
76 
78  ui->animList->setCurrentItem(0);
79  setCurrent(0);
80 }
81 
82 void KbAnimWidget::addAnim(const AnimScript* base, const QStringList& keyList, const QString& name, const QMap<QString, QVariant>& preset){
83  if(!light)
84  return;
85  noReorder = true;
86  KbAnim* animation = light->addAnim(base, keyList, name, preset);
87  QListWidgetItem* item = new QListWidgetItem(animation->name(), ui->animList);
88  item->setData(Qt::UserRole, animation->guid());
89  item->setFlags(item->flags() | Qt::ItemIsEditable);
90  animations[animation->guid()] = animation;
91  ui->animList->addItem(item);
92  ui->animList->setCurrentItem(item);
93  ui->animList->setVisible(true);
94  ui->noAnimLabel->setVisible(false);
95 
96  setCurrent(animation);
97  noReorder = false;
98 
99  // Activate settings dialog
101 }
102 
104  if(!light)
105  return;
106  noReorder = true;
107  KbAnim* animation = light->duplicateAnim(old);
108  // Refresh the list. insertItem doesn't seem to place the item in the correct position on its own...
109  refreshList();
110  ui->animList->setCurrentRow(light->animList().indexOf(animation));
111  ui->animList->setVisible(true);
112  ui->noAnimLabel->setVisible(false);
113 
114  setCurrent(animation);
115  noReorder = false;
116 }
117 
119  if(newCurrent != current)
120  emit animChanged(current = newCurrent);
121  if(!current){
122  selectedKeys.clear();
123  ui->selectionStack->setCurrentIndex(0);
124  return;
125  }
127  const AnimScript* script = current->script();
128  if(!script){
129  ui->selectionStack->setCurrentIndex(2);
130  ui->aMissingLabel->setText("The \"" + current->scriptName() + "\" script could not be loaded. Please check your animation directory.");
131  return;
132  }
133  ui->selectionStack->setCurrentIndex(1);
134  ui->aNameLabel->setText(script->name());
135  ui->aVerLabel->setText("v" + script->version());
136  ui->aCopyLabel->setText(script->copyright());
137 
138  ui->nameBox->setText(current->name());
139  ui->opacityBox->setValue(current->opacity() * 100.);
140  ui->blendBox->setCurrentIndex((int)current->mode());
141 }
142 
143 void KbAnimWidget::setSelectedKeys(const QStringList& keys){
144  selectedKeys = keys;
145  if(keys.count() == 0)
146  ui->keyButton->setVisible(false);
147  else
148  ui->keyButton->setVisible(true);
149 }
150 
151 void KbAnimWidget::on_animList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous){
152  if(!current)
153  setCurrent(0);
154  else
155  setCurrent(animations[current->data(Qt::UserRole).toUuid()]);
156 }
157 
158 void KbAnimWidget::on_animList_itemChanged(QListWidgetItem *item){
159  if(item){
160  KbAnim* anim = animations[item->data(Qt::UserRole).toUuid()];
161  if(anim){
162  anim->name(item->text().trimmed());
163  if(anim == current && !noReorder)
164  ui->nameBox->setText(anim->name());
165  }
166  }
167 }
168 
170  QListWidgetItem* item = ui->animList->itemAt(pos);
171  if(!item)
172  return;
173  setCurrent(animations[item->data(Qt::UserRole).toUuid()]);
174 
175  QMenu menu(this);
176  QAction* rename = new QAction("Rename...", this);
177  QAction* duplicate = new QAction("Duplicate", this);
178  QAction* del = new QAction("Delete", this);
179  menu.addAction(rename);
180  menu.addAction(duplicate);
181  menu.addAction(del);
182  QAction* result = menu.exec(QCursor::pos());
183  if(result == rename)
184  ui->animList->editItem(item);
185  else if(result == duplicate)
187  else if(result == del)
189 }
190 
191 void KbAnimWidget::on_nameBox_textEdited(const QString &arg1){
192  if(current){
193  noReorder = true;
194  current->name(arg1.trimmed());
195  ui->animList->currentItem()->setText(current->name());
196  noReorder = false;
197  }
198 }
199 
201  if(current)
202  current->opacity(arg1 / 100.);
203 }
204 
206  if(current)
207  current->mode((KbAnim::Mode)index);
208 }
209 
211  if(current){
212  QStringList keys = selectedKeys;
213  // If any keys were selected previously that aren't in the keymap now, leave them in
214  // This is important for layout compatibility - e.g. being able to select both bslash_iso and bslash, because no single layout contains both
215  const KeyMap& map = light->map();
216  foreach(const QString& key, current->keys()){
217  if(!map.contains(key))
218  keys << key;
219  }
220  current->keys(keys);
222  }
224 }
225 
227  if(current){
228  animations.remove(current->guid());
229  QList<KbAnim*> animList = light->animList();
230  animList.removeAll(current);
231  light->animList(animList);
232  current->deleteLater();
233  setCurrent(0);
234  delete ui->animList->currentItem();
235  if(animations.count() == 0){
236  ui->animList->setVisible(false);
237  ui->noAnimLabel->setVisible(true);
238  }
239  }
241 }
242 
244  if(!current)
245  return;
246  // Present animation property popup
247  AnimSettingDialog dialog(this, current);
248  dialog.exec();
249  if(dialog.result() != QDialog::Accepted){
250  current->resetParams();
251  return;
252  }
253  // Apply settings and restart all animations
255  current->reInit();
257  // Update name
258  ui->nameBox->setText(dialog.name());
259  on_nameBox_textEdited(dialog.name());
260 }
void on_propertyButton_clicked()
rgb * current
Definition: main.c:46
KbAnimWidget(QWidget *parent=0)
Definition: kbanimwidget.cpp:7
KbAnim * duplicateAnim(KbAnim *oldAnim)
Definition: kblight.cpp:148
QLabel * aMissingLabel
void setCurrent(KbAnim *newCurrent)
void on_nameBox_textEdited(const QString &arg1)
const QStringList & keys()
Definition: kbanim.h:40
QLineEdit * nameBox
void on_deleteButton_clicked()
const AnimList & animList()
Definition: kblight.h:49
Mode mode() const
Definition: kbanim.h:77
KbLight * light
Definition: kbanimwidget.h:52
KbAnim * addAnim(const AnimScript *base, const QStringList &keys, const QString &name, const QMap< QString, QVariant > &preset)
Definition: kblight.cpp:100
QLabel * noAnimLabel
QStackedWidget * selectionStack
const QString & name() const
Definition: kbanim.h:73
void setLight(KbLight *newLight)
void reorderAnims()
QString name() const
bool contains(const QString &name) const
Definition: keymap.h:143
Definition: keymap.h:49
RListWidget * animList
QComboBox * blendBox
void on_animList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
void on_blendBox_activated(int index)
const QString & scriptName() const
Definition: kbanim.h:82
QString copyright() const
Definition: animscript.h:62
const KeyMap & map()
Definition: kblight.h:28
QStringList selectedKeys
Definition: kbanimwidget.h:57
KbAnim * current
Definition: kbanimwidget.h:55
void refreshList()
void duplicateAnim(KbAnim *old)
void addAnim(const AnimScript *base, const QStringList &keyList, const QString &name, const QMap< QString, QVariant > &preset)
void setupUi(QWidget *KbAnimWidget)
Definition: kbanim.h:11
const QUuid & guid() const
Definition: kbanim.h:71
void on_animList_itemChanged(QListWidgetItem *item)
const QString & name() const
Definition: animscript.h:60
void animChanged(KbAnim *selection)
void reInit()
Definition: kbanim.cpp:149
void setSelectedKeys(const QStringList &keys)
float opacity() const
Definition: kbanim.h:75
Definition: keymap.h:49
void on_opacityBox_valueChanged(double arg1)
void restartAnimation()
Definition: kblight.cpp:179
QHash< QUuid, KbAnim * > animations
Definition: kbanimwidget.h:53
void clearSelection()
void on_keyButton_clicked()
void didUpdateSelection(QStringList keys)
void resetParams()
Definition: kbanim.cpp:127
const QString & version() const
Definition: animscript.h:61
QPushButton * keyButton
Mode
Definition: kbanim.h:17
const AnimScript * script() const
Definition: kbanim.h:81
void on_animList_customContextMenuRequested(const QPoint &pos)
Ui::KbAnimWidget * ui
Definition: kbanimwidget.h:60
QDoubleSpinBox * opacityBox
struct keyAnim * anim
Definition: main.c:55
void commitParams()
Definition: kbanim.cpp:121