-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpathaspect.cpp
More file actions
152 lines (126 loc) · 4.76 KB
/
pathaspect.cpp
File metadata and controls
152 lines (126 loc) · 4.76 KB
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
/* Copyright 2020 Pascal COMBES <pascom@orange.fr>
*
* This file is part of QtcDevPlugin.
*
* QtcDevPlugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtcDevPlugin 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtcDevPlugin. If not, see <http://www.gnu.org/licenses/>
*/
#include "pathaspect.h"
#include "Widgets/filetypevalidatinglineedit.h"
#include <utils/variablechooser.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>
#include <QtWidgets>
namespace QtcDevPlugin {
namespace Internal {
void PathAspect::fromMap(const Utils::Store& map)
{
QTC_ASSERT(!settingsKey().isEmpty(), return);
mValue = Utils::FilePath::fromSettings(map.value(settingsKey(), mDefaultValue.toSettings()));
qDebug() << __func__ << settingsKey().toByteArray() << "value:" << mValue;
}
void PathAspect::toMap(Utils::Store& map) const
{
QTC_ASSERT(!settingsKey().isEmpty(), return);
if (mValue != mDefaultValue)
map.insert(settingsKey(), mValue.toSettings());
}
void PathAspect::setDefaultValue(const Utils::FilePath& defaultValue)
{
bool isDefault = (mValue == mDefaultValue);
mDefaultValue = defaultValue;
if (isDefault)
mValue = defaultValue;
}
void PathAspect::setValue(const Utils::FilePath& value)
{
bool same = (mValue == value);
mValue = value;
qDebug() << __func__ << settingsKey().toByteArray() << "value:" << mValue;
if (!same)
emit changed();
}
void PathAspect::addToLayoutImpl(Layouting::Layout& builder)
{
mEdit = createSubWidget<Widgets::FileTypeValidatingLineEdit>();
mEdit->setMacroExpander(mMacroExpanderProvider());
mEdit->setAcceptFlags(mAccepted);
mEdit->setText(mValue.nativePath());
if (mMacroExpanderProvider != nullptr) {
Utils::VariableChooser* variableChooser = new Utils::VariableChooser(builder.emerge());
variableChooser->addSupportedWidget(mEdit);
variableChooser->addMacroExpanderProvider(mMacroExpanderProvider);
}
mButton = new QPushButton(tr("Browse..."));
connect(mEdit, SIGNAL(validChanged(bool)), this, SLOT(update(bool)));
connect(mEdit, SIGNAL(editingFinished()), this, SLOT(update()));
connect(mButton, SIGNAL(released()), this, SLOT(browse()));
QLayout* fieldLayout = new QHBoxLayout;
fieldLayout->addWidget(mEdit);
fieldLayout->addWidget(mButton);
if (mCheckable) {
mCheckbox = createSubWidget<QCheckBox>(displayName() + ":");
mCheckbox->setChecked(mValue != mDefaultValue);
mEdit->setEnabled(mCheckbox->isChecked());
mButton->setEnabled(mCheckbox->isChecked());
connect(mCheckbox, SIGNAL(toggled(bool)), this, SLOT(updateState(bool)));
builder.addItem(mCheckbox);
builder.addItem(fieldLayout);
} else {
mLabel = createSubWidget<QLabel>(displayName() + ":");
mLabel->setBuddy(mEdit);
builder.addItem(mLabel);
builder.addItem(fieldLayout);
}
}
void PathAspect::manageAcceptFlags(Widgets::FileTypeValidatingLineEdit::Accept flag, bool enable)
{
if (enable)
mAccepted |= flag;
else
mAccepted &= ~Widgets::FileTypeValidatingLineEdit::Accepts(flag);
}
void PathAspect::update(void)
{
if (mEdit->isValid())
setValue(Utils::FilePath::fromUserInput(mEdit->text()));
mEdit->setText(mValue.nativePath());
}
void PathAspect::update(bool valid)
{
if (valid)
setValue(Utils::FilePath::fromUserInput(mEdit->text()));
}
void PathAspect::updateState(bool checked)
{
mEdit->setEnabled(checked);
mButton->setEnabled(checked);
setValue(checked ? Utils::FilePath::fromUserInput(mEdit->text()) : Utils::FilePath());
}
void PathAspect::browse(void)
{
QString path;
if (mAccepted & Widgets::FileTypeValidatingLineEdit::AcceptsDirectories) {
path = QFileDialog::getExistingDirectory(nullptr, displayName(), mValue.nativePath());
} else if (mAccepted & Widgets::FileTypeValidatingLineEdit::AcceptsFiles) {
if (mAccepted & Widgets::FileTypeValidatingLineEdit::AcceptNew)
path = QFileDialog::getSaveFileName(nullptr, displayName(), mValue.nativePath());
else
path = QFileDialog::getOpenFileName(nullptr, displayName(), mValue.nativePath());
}
if (!path.isNull())
setValue(Utils::FilePath::fromString(path));
mEdit->setText(mValue.nativePath());
}
} // Internal
} // QtcDevPlugin