-
Notifications
You must be signed in to change notification settings - Fork 0
/
destinationwidget.cpp
121 lines (110 loc) · 3.95 KB
/
destinationwidget.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
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
/* This file is part of StarBackup
* StarBackup is a Free Real-Time Backup and Archiving Software
*
* Copyright (C) 2011 Clément Nardi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "destinationwidget.h"
#include <QFileDialog>
#include "trace.h"
#include <QDesktopServices>
DestinationLineWidget::DestinationLineWidget(QWidget *parent) :
QWidget(parent)
{
removeButton = new QPushButton(QIcon(":/icons/remove"),"");
removeButton->setToolTip(tr("Remove this destination folder"));
editButton = new QPushButton(QIcon(":/icons/folder"),"");
editButton->setToolTip(tr("Choose destination folder"));
destLine = new QLineEdit();
destLine->setEnabled(false);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(destLine);
layout->addWidget(editButton);
layout->addWidget(removeButton);
this->setMinimumHeight(20);
layout->setMargin(0);
setLayout(layout);
connect(editButton,SIGNAL(clicked()),this,SLOT(browseFolder()));
connect(removeButton,SIGNAL(clicked()),this,SLOT(eraseFolder()));
}
void DestinationLineWidget::browseFolder(){
QString dir = destLine->text();
dir = QFileDialog::getExistingDirectory(this,
tr("Choose the destination directory"),
QDesktopServices::storageLocation(QDesktopServices::DesktopLocation),
QFileDialog::ShowDirsOnly);
if (dir.size()>0) {
QString path = QDir(dir).canonicalPath();
destLine->setText(path);
}
}
void DestinationLineWidget::eraseFolder(){
destLine->setText("");
}
DestinationWidget::DestinationWidget(QWidget *parent) :
QWidget(parent) {
lines = new QList<DestinationLineWidget *>();
layout = new QVBoxLayout(this);
layout->setMargin(3);
updateLines();
layout->addStretch(10);
setLayout(layout);
}
void DestinationWidget::addLine(QString str){
DestinationLineWidget * line = new DestinationLineWidget(this);
lines->append(line);
layout->insertWidget(lines->size()-1,line);
line->destLine->setText(str);
connect(line->destLine,SIGNAL(textChanged(QString)),this, SLOT(updateLines()));
}
void DestinationWidget::updateLines() {
int nbEmpty = 0;
for (int i = 0; i<lines->size(); i++) {
if (lines->at(i)->destLine->text().size() == 0) {
nbEmpty++;
}
lines->at(i)->destLine->setToolTip(lines->at(i)->destLine->text());
lines->at(i)->destLine->setCursorPosition(0);
}
TRACE(qDebug()<<nbEmpty);
for (int i = 0; i<lines->size(); i++) {
if (lines->at(i)->destLine->text().size() == 0) {
if (nbEmpty>1) {
DestinationLineWidget *tempLine = lines->at(i);
layout->removeWidget(tempLine);
lines->removeAt(i);
i--;
tempLine->deleteLater();
nbEmpty--;
}
}
}
if (nbEmpty == 0) {
addLine();
}
dataChanged();
}
void DestinationWidget::resetDirs(QList<DestFolderInfo> list){
while (lines->size()>0){
DestinationLineWidget *tempLine = lines->at(0);
layout->removeWidget(tempLine);
lines->removeAt(0);
tempLine->deleteLater();
}
for (int i = 0; i<list.size(); i++) {
addLine(list.at(i));
}
updateLines();
}