summaryrefslogtreecommitdiffstats
path: root/configurationdialog.cpp
blob: f3e00095e9b7bb9e650dbae7970ab10e1773b61a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QTabWidget>
#include <QSettings>
#include <QColorDialog>
#include <QGroupBox>
#include <QRadioButton>

#include "configurationdialog.h"
#include "helper.h"

ConfigurationDialog::ConfigurationDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) {
    //database setup
    QGridLayout *dbLayout = new QGridLayout;
    mDbHost = new QLineEdit;
    dbLayout->addWidget(new QLabel(tr("Hostname")), 0, 0);
    dbLayout->addWidget(mDbHost, 0, 1);
    mDbUser = new QLineEdit;
    dbLayout->addWidget(new QLabel(tr("Username")), 1, 0);
    dbLayout->addWidget(mDbUser, 1, 1);
    mDbPass = new QLineEdit;
    mDbPass->setEchoMode(QLineEdit::Password);
    dbLayout->addWidget(new QLabel(tr("Password")), 2, 0);
    dbLayout->addWidget(mDbPass, 2, 1);
    mDbName = new QLineEdit;
    dbLayout->addWidget(new QLabel(tr("DB Name")), 3, 0);
    dbLayout->addWidget(mDbName, 3, 1);
    dbLayout->setAlignment(Qt::AlignTop);
    QWidget *dbWidget = new QWidget;
    dbWidget->setLayout(dbLayout);

    //buttons
    QPushButton *acceptB = new QPushButton(tr("Save"));
    connect(acceptB, &QPushButton::clicked, this, &ConfigurationDialog::accept);
    QPushButton *cancelB = new QPushButton(tr("Cancel"));
    connect(cancelB, &QPushButton::clicked, this, &ConfigurationDialog::reject);
    QHBoxLayout *bLayout = new QHBoxLayout;
    bLayout->addStretch();
    bLayout->addWidget(acceptB);
    bLayout->addWidget(cancelB);
    bLayout->addStretch();

    //colors
    QWidget *colorWidget = new QWidget;
    QGridLayout *colorGrid = new QGridLayout;
    colorGrid->setColumnStretch(1, 4);
    colorGrid->addWidget(new QLabel(tr("Base color")), 0, 0);
    mBaseColorL = new QLabel;
    mBaseColorL->setFrameStyle(QFrame::Sunken | QFrame::Panel);
    mBaseColorL->setScaledContents(true);
    colorGrid->addWidget(mBaseColorL, 0, 1);
    QPushButton *baseColorB = new QPushButton;
    baseColorB->setIcon(QIcon(":/fill-color.png"));
    colorGrid->addWidget(baseColorB, 0, 2);
    colorGrid->addWidget(new QLabel(tr("Alt. color")), 1, 0);
    mAltColorL = new QLabel;
    mAltColorL->setFrameStyle(QFrame::Sunken | QFrame::Panel);
    mAltColorL->setScaledContents(true);
    colorGrid->addWidget(mAltColorL, 1, 1);
    QPushButton *altColorB = new QPushButton;
    altColorB->setIcon(QIcon(":/fill-color.png"));
    colorGrid->addWidget(altColorB, 1, 2);
    QVBoxLayout *colorL = new QVBoxLayout;
    colorL->addLayout(colorGrid);
    colorL->addStretch();
    colorWidget->setLayout(colorL);
    mUseAltColors = new QCheckBox(tr("Use alt. row colors"));
    colorGrid->addWidget(mUseAltColors, 2, 1, 1, 2, Qt::AlignLeft);
    connect(baseColorB, &QPushButton::clicked, [=] { chooseColor(mBaseColorL); });
    connect(altColorB, &QPushButton::clicked, [=] { chooseColor(mAltColorL); });

    //scan mode
    QWidget *scanModeWidget = new QWidget;
    QGroupBox *scanGB = new QGroupBox(tr("Scan Mode"));
    mUseBeetRB = new QRadioButton(tr("Use beet ls"));
    QRadioButton *scanDirRB = new QRadioButton(tr("Scan directory"));
    scanDirRB->setChecked(true);
    QHBoxLayout *scanGBL = new QHBoxLayout;
    scanGBL->addWidget(mUseBeetRB);
    scanGBL->addWidget(scanDirRB);
    scanGB->setLayout(scanGBL);
    QGridLayout *scanDirL = new QGridLayout;
    mScanDir = new QLineEdit;
    scanDirL->addWidget(new QLabel(tr("Dir to scan")), 0, 0);
    scanDirL->addWidget(mScanDir, 1, 0);
    QVBoxLayout *scanModeL = new QVBoxLayout;
    scanModeL->addWidget(scanGB);
    scanModeL->addLayout(scanDirL);
    scanModeWidget->setLayout(scanModeL);
    connect(mUseBeetRB, &QRadioButton::toggled, this, &ConfigurationDialog::selectBeet);

    //dialog layout
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QTabWidget *tab = new QTabWidget;
    tab->addTab(dbWidget, tr("Database"));
    tab->addTab(colorWidget, tr("Colors"));
    tab->addTab(scanModeWidget, tr("Scan Mode"));
    mainLayout->addWidget(tab);
    mainLayout->addLayout(bLayout);
    readSettings();
    setLayout(mainLayout);
    setMinimumWidth(400);
}

void ConfigurationDialog::accept(){
    QSettings s;
    s.setValue("dbhost", mDbHost->text());
    s.setValue("dbuser", mDbUser->text());
    s.setValue("dbpass", mDbPass->text());
    s.setValue("dbname", mDbName->text());
    QColor baseColor = Helper::colorFromLabel(mBaseColorL);
    s.setValue("basecolor", baseColor);
    QColor altColor = Helper::colorFromLabel(mAltColorL);
    s.setValue("altcolor", altColor);
    s.setValue("usealtcolors", mUseAltColors->isChecked());
    s.setValue("usebeet", mUseBeetRB->isChecked());
    s.setValue("scandir", mScanDir->text());
    QDialog::accept();
}

void ConfigurationDialog::chooseColor(QWidget *label){
    QLabel *curLabel = static_cast<QLabel*>(label);
    QColor startColor = Helper::colorFromLabel(curLabel);
    if(!startColor.isValid()){
        startColor = palette().base().color();
    }
    QColor newColor = QColorDialog::getColor(startColor, this);
    if(newColor.isValid()){
        QPixmap newPm(curLabel->sizeHint());
        newPm.fill(newColor);
        curLabel->setPixmap(newPm);
    }
}

void ConfigurationDialog::selectBeet(bool checked){
    if(checked){
        mScanDir->setEnabled(false);
    }else{
        mScanDir->setEnabled(true);
    }
}

void ConfigurationDialog::readSettings(){
    QSettings s;
    mDbHost->setText(s.value("dbhost").toString());
    mDbUser->setText(s.value("dbuser").toString());
    mDbPass->setText(s.value("dbpass").toString());
    mDbName->setText(s.value("dbname").toString());
    QVariant baseColorV = s.value("basecolor", palette().base().color());
    QColor baseColor = baseColorV.value<QColor>();
    Helper::fillLabel(mBaseColorL, baseColor);
    QVariant altColorV = s.value("altcolor", palette().alternateBase().color());
    QColor altColor = altColorV.value<QColor>();
    Helper::fillLabel(mAltColorL, altColor);
    bool useAltColors = s.value("usealtcolors", false).toBool();
    mUseAltColors->setChecked(useAltColors);
    bool useBeet = s.value("usebeet", true).toBool();
    mUseBeetRB->setChecked(useBeet);
    QString scanDir = s.value("scandir", tr("<empty>")).toString();
    mScanDir->setText(scanDir);
}