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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* This file is part of KDevelop
Copyright 2008 Hamish Rodda <rodda@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "outputpage.h"
#include "ui_outputlocation.h"
#include "debug.h"
#include <language/codegen/sourcefiletemplate.h>
#include <language/codegen/templaterenderer.h>
#include <KConfigGroup>
#include <KSharedConfig>
#include <KLocalizedString>
#include <KUrlRequester>
#include <QSpinBox>
#include <QLabel>
namespace KDevelop {
struct OutputPagePrivate
{
explicit OutputPagePrivate(OutputPage* page_)
: page(page_)
, output(nullptr)
{ }
OutputPage* page;
Ui::OutputLocationDialog* output;
QHash<QString, KUrlRequester*> outputFiles;
QHash<QString, QSpinBox*> outputLines;
QHash<QString, QSpinBox*> outputColumns;
QList<QLabel*> labels;
QHash<QString, QUrl> defaultUrls;
QHash<QString, QUrl> lowerCaseUrls;
QStringList fileIdentifiers;
void updateRanges(QSpinBox* line, QSpinBox* column, bool enable);
/**
* This implementation simply enables the position widgets on a file that exists.
* Derived classes should overload to set the ranges where class generation should be allowed
*
* @param field the name of the file to be generated (Header, Implementation, etc)
*/
void updateFileRange(const QString& field);
void updateFileNames();
void validate();
};
void OutputPagePrivate::updateRanges(QSpinBox* line, QSpinBox* column, bool enable)
{
qCDebug(PLUGIN_FILETEMPLATES) << "Updating Ranges, file exists: " << enable;
line->setEnabled(enable);
column->setEnabled(enable);
}
void OutputPagePrivate::updateFileRange(const QString& field)
{
const auto outputFileIt = outputFiles.constFind(field);
if (outputFileIt == outputFiles.constEnd()) {
return;
}
const QString url = (*outputFileIt)->url().toLocalFile();
QFileInfo info(url);
updateRanges(outputLines[field], outputColumns[field], info.exists() && !info.isDir());
validate();
}
void OutputPagePrivate::updateFileNames()
{
bool lower = output->lowerFilenameCheckBox->isChecked();
const QHash<QString, QUrl> urls = lower ? lowerCaseUrls : defaultUrls;
for (QHash<QString, KUrlRequester*>::const_iterator it = outputFiles.constBegin();
it != outputFiles.constEnd(); ++it)
{
const QUrl url = urls.value(it.key());
if (!url.isEmpty())
{
it.value()->setUrl(url);
}
}
//Save the setting for next time
KConfigGroup codegenGroup( KSharedConfig::openConfig(), "CodeGeneration" );
codegenGroup.writeEntry( "LowerCaseFilenames", output->lowerFilenameCheckBox->isChecked() );
validate();
}
void OutputPagePrivate::validate()
{
QStringList invalidFiles;
for(QHash< QString, KUrlRequester* >::const_iterator it = outputFiles.constBegin();
it != outputFiles.constEnd(); ++it)
{
if (!it.value()->url().isValid()) {
invalidFiles << it.key();
} else if (it.value()->url().isLocalFile() && !QFileInfo(it.value()->url().adjusted(QUrl::RemoveFilename).toLocalFile()).isWritable()) {
invalidFiles << it.key();
}
}
bool valid = invalidFiles.isEmpty();
if (valid) {
output->messageWidget->animatedHide();
} else {
std::sort(invalidFiles.begin(), invalidFiles.end());
output->messageWidget->setMessageType(KMessageWidget::Error);
output->messageWidget->setCloseButtonVisible(false);
output->messageWidget->setText(i18np("Invalid output file: %2", "Invalid output files: %2", invalidFiles.count(), invalidFiles.join(QLatin1String(", "))));
output->messageWidget->animatedShow();
}
emit page->isValid(valid);
}
OutputPage::OutputPage(QWidget* parent)
: QWidget(parent)
, d(new OutputPagePrivate(this))
{
d->output = new Ui::OutputLocationDialog;
d->output->setupUi(this);
d->output->messageWidget->setVisible(false);
connect(d->output->lowerFilenameCheckBox, &QCheckBox::stateChanged,
this, [&] { d->updateFileNames(); });
}
OutputPage::~OutputPage()
{
delete d->output;
delete d;
}
void OutputPage::prepareForm(const SourceFileTemplate& fileTemplate)
{
// First clear any existing file configurations
// This can happen when going back and forth between assistant pages
d->fileIdentifiers.clear();
d->defaultUrls.clear();
d->lowerCaseUrls.clear();
while (d->output->urlFormLayout->count() > 0)
{
d->output->urlFormLayout->takeAt(0);
}
while (d->output->positionFormLayout->count() > 0)
{
d->output->positionFormLayout->takeAt(0);
}
qDeleteAll(d->outputFiles);
qDeleteAll(d->outputLines);
qDeleteAll(d->outputColumns);
qDeleteAll(d->labels);
d->outputFiles.clear();
d->outputLines.clear();
d->outputColumns.clear();
d->labels.clear();
const auto outputFiles = fileTemplate.outputFiles();
const int outputFilesCount = outputFiles.count();
d->output->urlGroupBox->setTitle(i18ncp("@title:group", "Output File", "Output Files", outputFilesCount));
d->output->positionGroupBox->setTitle(i18ncp("@title:group", "Location within Existing File", "Location within Existing Files", outputFilesCount));
for (const SourceFileTemplate::OutputFile& file : outputFiles) {
const QString id = file.identifier;
d->fileIdentifiers << id;
const QString fileLabelText = i18nc("@label:chooser file name arg", "%1:", file.label);
auto* label = new QLabel(fileLabelText, this);
d->labels << label;
auto* requester = new KUrlRequester(this);
requester->setMode( KFile::File | KFile::LocalOnly );
connect(requester, &KUrlRequester::textChanged, this, [this, id] () { d->updateFileRange(id); });
d->output->urlFormLayout->addRow(label, requester);
d->outputFiles.insert(file.identifier, requester);
label = new QLabel(fileLabelText, this);
d->labels << label;
auto* layout = new QHBoxLayout;
auto line = new QSpinBox(this);
line->setPrefix(i18n("Line: "));
line->setValue(0);
line->setMinimum(0);
layout->addWidget(line);
auto column = new QSpinBox(this);
column->setPrefix(i18n("Column: "));
column->setValue(0);
column->setMinimum(0);
layout->addWidget(column);
d->output->positionFormLayout->addRow(label, layout);
d->outputLines.insert(file.identifier, line);
d->outputColumns.insert(file.identifier, column);
}
}
void OutputPage::loadFileTemplate(const SourceFileTemplate& fileTemplate,
const QUrl& _baseUrl,
TemplateRenderer* renderer)
{
QUrl baseUrl = _baseUrl;
if (!baseUrl.path().endsWith(QLatin1Char('/'))) {
baseUrl.setPath(baseUrl.path() + QLatin1Char('/'));
}
KConfigGroup codegenGroup( KSharedConfig::openConfig(), "CodeGeneration" );
bool lower = codegenGroup.readEntry( "LowerCaseFilenames", true );
d->output->lowerFilenameCheckBox->setChecked(lower);
const auto outputFiles = fileTemplate.outputFiles();
for (const SourceFileTemplate::OutputFile& file : outputFiles) {
d->fileIdentifiers << file.identifier;
QUrl url = baseUrl.resolved(QUrl(renderer->render(file.outputName)));
d->defaultUrls.insert(file.identifier, url);
url = baseUrl.resolved(QUrl(renderer->render(file.outputName).toLower()));
d->lowerCaseUrls.insert(file.identifier, url);
}
d->updateFileNames();
}
QHash< QString, QUrl > OutputPage::fileUrls() const
{
QHash<QString, QUrl> urls;
for (QHash<QString, KUrlRequester*>::const_iterator it = d->outputFiles.constBegin(); it != d->outputFiles.constEnd(); ++it)
{
urls.insert(it.key(), it.value()->url());
}
return urls;
}
QHash< QString, KTextEditor::Cursor > OutputPage::filePositions() const
{
QHash<QString, KTextEditor::Cursor> positions;
for (const QString& identifier : qAsConst(d->fileIdentifiers)) {
positions.insert(identifier, KTextEditor::Cursor(d->outputLines[identifier]->value(), d->outputColumns[identifier]->value()));
}
return positions;
}
void OutputPage::setFocusToFirstEditWidget()
{
d->output->lowerFilenameCheckBox->setFocus();
}
}
#include "moc_outputpage.cpp"
|