forked from lindenle/QTpapers
/
LibraryDialog.cpp
executable file
·98 lines (84 loc) · 2.75 KB
/
LibraryDialog.cpp
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
#include "LibraryDialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QSettings>
#include <QDir>
#include <QMessageBox>
#include <stdlib.h>
LibraryDialog::LibraryDialog(QWidget * parent) :
QDialog(parent)
{
setWindowTitle(tr("New Library"));
_library = new QLabel(tr("Library Name"));
_libraryname = new QLineEdit;
_library->setBuddy(_libraryname);
_directory = new QLabel(tr("Directory Location"));
_directoryname = new QLineEdit;
//set a default directory from the settings
QSettings settings;
settings.beginGroup("LastLibrary");
_directoryname->setText( settings.value("directory",QString(getenv("HOME"))).toString());
settings.endGroup();
_directory->setBuddy(_directoryname);
_search = new QPushButton(tr("Search"));
connect(_search, SIGNAL(clicked()), this, SLOT(open_directory_search()));
QWidget * directory = new QWidget;
QHBoxLayout * directory_layout = new QHBoxLayout;
directory_layout->addWidget( _directory);
directory_layout->addWidget( _directoryname);
directory_layout->addWidget( _search);
directory->setLayout(directory_layout);
QWidget * buttons = new QWidget;
_ok = new QPushButton(tr("OK"));
connect(_ok, SIGNAL(clicked()), this, SLOT(ok_clicked()));
_cancel = new QPushButton(tr("CANCEL"));
connect(_cancel, SIGNAL(clicked()), this, SLOT(reject()));
QHBoxLayout * bottom_buttons = new QHBoxLayout;
bottom_buttons->addWidget(_ok);
bottom_buttons->addWidget(_cancel);
buttons->setLayout(bottom_buttons);
QVBoxLayout * topdown = new QVBoxLayout();
topdown->addWidget(_library);
topdown->addWidget(_libraryname);
topdown->addWidget(directory);
topdown->addWidget(buttons);
setLayout(topdown);
//here we need to set some signals
}
void LibraryDialog::open_directory_search()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::DirectoryOnly);
dialog.setViewMode(QFileDialog::Detail);
dialog.hasFocus();
dialog.show();
QStringList fileNames;
if (dialog.exec())
{
fileNames = dialog.selectedFiles();
_directoryname->setText(fileNames.at(0));
}
}
void LibraryDialog::ok_clicked()
{
//stor the directory and the filename into the Library check to see
//if null
_library_pointer->set_library_name(_libraryname->text());
//should we check if the directory exists
QDir directory(_directoryname->text());
if ( ! directory.exists() )
{
QString question(tr("Directory %1 does not exist. Creat it?").arg(_directoryname->text()));
if ( QMessageBox::warning(this,"Create Directory",
question,
QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
{
directory.mkdir(directory.path());
}
else
reject();
}
_library_pointer->set_library_directory(_directoryname->text());
accept();
}