1 #include "add_label_dialog.h"
2 
AddLabelDialog(QWidget * parent)3 AddLabelDialog::AddLabelDialog(QWidget *parent)
4     : QDialog(parent)
5 {
6     auto layout = new QVBoxLayout;
7 
8     layout->addWidget(new QLabel(tr("Label name:")));
9     layout->addWidget(edit = new QLineEdit());
10 
11     layout->addWidget(new QLabel(tr("Choose a color:")));
12     layout->addWidget(list = new QListWidget());
13 
14     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_red.png"), tr("red")));
15     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_orange.png"), tr("orange")));
16     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_yellow.png"), tr("yellow")));
17     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_green.png"), tr("green")));
18     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_cyan.png"), tr("cyan")));
19     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_blue.png"), tr("blue")));
20     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_violet.png"), tr("violet")));
21     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_purple.png"), tr("purple")));
22     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_pink.png"), tr("pink")));
23     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_white.png"), tr("white")));
24     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_light.png"), tr("light")));
25     list->addItem(new QListWidgetItem(QIcon(":/images/lists/label_dark.png"), tr("dark")));
26 
27     QColor backgroundColor = this->palette().background().color();
28     list->setStyleSheet(QString("QListWidget {border : none; background-color: rgb(%1,%2,%3);}").arg(backgroundColor.red()).arg(backgroundColor.green()).arg(backgroundColor.blue()));
29     list->setMinimumHeight(225);
30 
31     setModal(true);
32 
33     setMinimumHeight(340);
34 
35     //buttons
36     acceptButton = new QPushButton(tr("accept"), this);
37     cancelButton = new QPushButton(tr("cancel"), this);
38 
39     auto buttons = new QHBoxLayout;
40     buttons->addStretch();
41     buttons->addWidget(acceptButton);
42     buttons->addWidget(cancelButton);
43 
44     layout->addStretch();
45     layout->addLayout(buttons);
46 
47     setLayout(layout);
48 
49     //connections
50     connect(edit, SIGNAL(textChanged(QString)), this, SLOT(validateName(QString)));
51     connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
52     connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
53 }
54 
selectedColor()55 YACReader::LabelColors AddLabelDialog::selectedColor()
56 {
57     return YACReader::LabelColors(list->currentRow() + 1);
58 }
59 
name()60 QString AddLabelDialog::name()
61 {
62     return edit->text();
63 }
64 
exec()65 int AddLabelDialog::exec()
66 {
67     edit->clear();
68     list->clearSelection();
69 
70     acceptButton->setDisabled(true);
71 
72     list->setCurrentRow(0);
73 
74     return QDialog::exec();
75 }
76 
validateName(const QString & name)77 void AddLabelDialog::validateName(const QString &name)
78 {
79     if (name.isEmpty())
80         acceptButton->setDisabled(true);
81     else
82         acceptButton->setEnabled(true);
83 }
84