Added sticky alerts
[kitchenalert] / src / stickydialog.cpp
1 #include "stickydialog.h"
2 #include <QVBoxLayout>
3 #include <QSettings>
4 #include <QPushButton>
5 #include <QFileDialog>
6 #include <QDebug>
7
8 StickyDialog::StickyDialog(QString defaultDirectory, QWidget *parent) :
9     QDialog(parent)
10 {
11     defaultDirectory_ = defaultDirectory;
12
13
14     QVBoxLayout* pMainLayout = new QVBoxLayout(this);
15
16     QSettings settings;
17     QStringList stickies = settings.value("sticky").toStringList();
18
19 //    stickies.append("Test String");
20
21     pStickiesModel_ = new QStringListModel;
22     pStickiesModel_->setStringList(stickies);
23
24     pStickiesView_ = new QListView;
25     pStickiesView_->setModel(pStickiesModel_);
26     pStickiesView_->setSelectionMode(QAbstractItemView::SingleSelection);
27     pMainLayout->addWidget(pStickiesView_);
28
29     QHBoxLayout * pButtonLayout = new QHBoxLayout;
30     pMainLayout->addLayout(pButtonLayout);
31
32     QPushButton *  pAddButton = new QPushButton (tr("Add"));
33     connect(pAddButton,SIGNAL(clicked()),this,SLOT(add()));
34     pButtonLayout->addWidget(pAddButton);
35
36     QPushButton * pRemoveButton = new QPushButton(tr("Remove"));
37     connect(pRemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
38     pButtonLayout->addWidget(pRemoveButton);
39
40     QPushButton * pOkButton = new QPushButton(tr("OK"));
41     connect (pOkButton,SIGNAL(clicked()),this,SLOT(accept()));
42     pButtonLayout->addWidget(pOkButton);
43 }
44
45 void StickyDialog::add()
46 {
47     QString startDirectory;
48
49     if (QFile(defaultDirectory_).exists())
50     {
51         startDirectory = defaultDirectory_;
52     }
53     else
54     {
55         startDirectory = "/home/user/";
56         qDebug () << "default save directory not found";
57     }
58
59
60     QString filename = QFileDialog::getOpenFileName(this,tr("KitchenAlert - Choose a timer to add to stickied"),startDirectory,tr("KitchenAlert timer files (*.kitchenalert)"));
61
62     if (filename.isEmpty()) // user cancelled the dialog
63         return;
64
65
66     QStringList tempList = pStickiesModel_->stringList();
67     tempList.append(filename);
68     pStickiesModel_->setStringList(tempList);
69
70 }
71
72 void StickyDialog::remove()
73 {
74     QItemSelectionModel* pSelected = pStickiesView_->selectionModel();
75     QModelIndex index = pSelected->currentIndex(); //Only single selection allowed, so we only need to care about current item
76
77
78     if (!index.isValid())
79         return;         //Nothing selected, nothing to remove...
80
81     pStickiesModel_->removeRows(index.row(),1);
82
83 }
84
85 QStringList StickyDialog::getStickyList()
86 {
87     return pStickiesModel_->stringList();
88 }