Commit bbd5d2dd authored by Thomas Hartmann's avatar Thomas Hartmann
Browse files

Wizards: allow forcing of first letter to be a capital letter



This adds a flags that enforces capitalization of filenames.

Change-Id: Ie6660f1985a2e96fd68549c5ced1b37c33f064cc
Reviewed-by: default avatarAlessandro Portale <alessandro.portale@nokia.com>
parent d86b2abc
......@@ -167,6 +167,9 @@ void BaseValidatingLineEdit::slotChanged(const QString &t)
emit validChanged();
}
}
bool block = blockSignals(true);
setText(fixInputString(t));
blockSignals(block);
}
void BaseValidatingLineEdit::slotReturnPressed()
......@@ -180,4 +183,9 @@ void BaseValidatingLineEdit::triggerChanged()
slotChanged(text());
}
QString BaseValidatingLineEdit::fixInputString(const QString &string)
{
return string;
}
} // namespace Utils
......@@ -77,6 +77,7 @@ signals:
protected:
virtual bool validate(const QString &value, QString *errorMessage) const = 0;
virtual QString fixInputString(const QString &string);
protected slots:
// Custom behaviour can be added here. The base implementation must
......
......@@ -68,7 +68,8 @@ static const QRegExp &windowsDeviceSubDirPattern()
// ----------- FileNameValidatingLineEdit
FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
BaseValidatingLineEdit(parent),
m_allowDirectories(false)
m_allowDirectories(false),
m_forceFirstCapitalLetter(false)
{
}
......@@ -82,6 +83,16 @@ void FileNameValidatingLineEdit::setAllowDirectories(bool v)
m_allowDirectories = v;
}
bool FileNameValidatingLineEdit::forceFirstCapitalLetter() const
{
return m_forceFirstCapitalLetter;
}
void FileNameValidatingLineEdit::setForceFirstCapitalLetter(bool b)
{
m_forceFirstCapitalLetter = b;
}
/* Validate a file base name, check for forbidden characters/strings. */
#ifdef Q_OS_WIN
......@@ -147,6 +158,17 @@ bool FileNameValidatingLineEdit::validate(const QString &value, QString *errorM
&& validateFileName(value, allowDirectories(), errorMessage);
}
QString FileNameValidatingLineEdit::fixInputString(const QString &string)
{
if (!forceFirstCapitalLetter())
return string;
QString fixedString = string;
if (!string.isEmpty() && string.at(0).isLower())
fixedString[0] = string.at(0).toUpper();
return fixedString;
}
bool FileNameValidatingLineEdit::validateFileNameExtension(const QString &fileName,
const QStringList &requiredExtensions,
......
......@@ -42,6 +42,7 @@ class QTCREATOR_UTILS_EXPORT FileNameValidatingLineEdit : public BaseValidatingL
Q_OBJECT
Q_PROPERTY(bool allowDirectories READ allowDirectories WRITE setAllowDirectories)
Q_PROPERTY(QStringList requiredExtensions READ requiredExtensions WRITE setRequiredExtensions)
Q_PROPERTY(bool forceFirstCapitalLetter READ forceFirstCapitalLetter WRITE setForceFirstCapitalLetter)
public:
explicit FileNameValidatingLineEdit(QWidget *parent = 0);
......@@ -61,6 +62,13 @@ public:
bool allowDirectories() const;
void setAllowDirectories(bool v);
/**
* Sets whether the first letter is forced to be a capital letter
* Default is off.
*/
bool forceFirstCapitalLetter() const;
void setForceFirstCapitalLetter(bool b);
/**
* Sets a requred extension. If the extension is empty no extension is required.
* Default is empty.
......@@ -70,10 +78,12 @@ public:
protected:
virtual bool validate(const QString &value, QString *errorMessage) const;
virtual QString fixInputString(const QString &string);
private:
bool m_allowDirectories;
QStringList m_requiredExtensionList;
bool m_forceFirstCapitalLetter;
};
} // namespace Utils
......
......@@ -75,6 +75,16 @@ QString FileWizardDialog::path() const
return m_filePage->path();
}
bool FileWizardDialog::forceFirstCapitalLetterForFileName() const
{
return m_filePage->forceFirstCapitalLetterForFileName();
}
void FileWizardDialog::setForceFirstCapitalLetterForFileName(bool b)
{
m_filePage->setForceFirstCapitalLetterForFileName(b);
}
void FileWizardDialog::setPath(const QString &path)
{
m_filePage->setPath(path);
......
......@@ -49,6 +49,9 @@ public:
QString fileName() const;
QString path() const;
bool forceFirstCapitalLetterForFileName() const;
void setForceFirstCapitalLetterForFileName(bool b);
public slots:
void setPath(const QString &path);
void setFileName(const QString &name);
......
......@@ -50,6 +50,7 @@ struct FileWizardPagePrivate
FileWizardPagePrivate();
Ui::WizardPage m_ui;
bool m_complete;
bool m_forceFirstCapitalLetter;
};
FileWizardPagePrivate::FileWizardPagePrivate() :
......@@ -121,6 +122,16 @@ void FileWizardPage::setPathLabel(const QString &label)
d->m_ui.pathLabel->setText(label);
}
bool FileWizardPage::forceFirstCapitalLetterForFileName() const
{
return d->m_ui.nameLineEdit->forceFirstCapitalLetter();
}
void FileWizardPage::setForceFirstCapitalLetterForFileName(bool b)
{
d->m_ui.nameLineEdit->setForceFirstCapitalLetter(b);
}
void FileWizardPage::slotValidChanged()
{
const bool newComplete = d->m_ui.pathChooser->isValid() && d->m_ui.nameLineEdit->isValid();
......
......@@ -59,6 +59,9 @@ public:
void setFileNameLabel(const QString &label);
void setPathLabel(const QString &label);
bool forceFirstCapitalLetterForFileName() const;
void setForceFirstCapitalLetterForFileName(bool b);
// Validate a base name entry field (potentially containing extension)
static bool validateBaseName(const QString &name, QString *errorMessage = 0);
......
......@@ -464,10 +464,17 @@ void BaseFileWizard::runWizard(const QString &path, QWidget *parent, const QStri
// Create dialog and run it. Ensure that the dialog is deleted when
// leaving the func, but not before the IFileWizardExtension::process
// has been called
WizardDialogParameters::DialogParameterFlags dialogParameterFlags;
if (flags().testFlag(ForceCapitalLetterForFileName))
dialogParameterFlags |= WizardDialogParameters::ForceCapitalLetterForFileName;
const QScopedPointer<QWizard> wizard(createWizardDialog(parent, WizardDialogParameters(path,
allExtensionPages,
platform,
requiredFeatures())));
requiredFeatures(),
dialogParameterFlags)));
QTC_ASSERT(!wizard.isNull(), return);
GeneratedFiles files;
......@@ -811,6 +818,8 @@ QWizard *StandardFileWizard::createWizardDialog(QWidget *parent,
const WizardDialogParameters &wizardDialogParameters) const
{
Utils::FileWizardDialog *standardWizardDialog = new Utils::FileWizardDialog(parent);
if (wizardDialogParameters.flags().testFlag(WizardDialogParameters::ForceCapitalLetterForFileName))
standardWizardDialog->setForceFirstCapitalLetterForFileName(true);
standardWizardDialog->setWindowTitle(tr("New %1").arg(displayName()));
setupWizard(standardWizardDialog);
standardWizardDialog->setPath(wizardDialogParameters.defaultPath());
......
......@@ -113,12 +113,19 @@ class CORE_EXPORT WizardDialogParameters
public:
typedef QList<QWizardPage *> WizardPageList;
enum DialogParameterEnum {
ForceCapitalLetterForFileName = 0x01
};
Q_DECLARE_FLAGS(DialogParameterFlags, DialogParameterEnum)
explicit WizardDialogParameters(const QString &defaultPath, const WizardPageList &extensionPages,
const QString &platform, const Core::FeatureSet &requiredFeatures)
const QString &platform, const Core::FeatureSet &requiredFeatures,
DialogParameterFlags flags)
: m_defaultPath(defaultPath),
m_extensionPages(extensionPages),
m_selectedPlatform(platform),
m_requiredFeatures(requiredFeatures) {}
m_requiredFeatures(requiredFeatures),
m_parameterFlags(flags) {}
QString defaultPath() const
{ return m_defaultPath; }
......@@ -132,11 +139,15 @@ public:
Core::FeatureSet requiredFeatures() const
{ return m_requiredFeatures; }
DialogParameterFlags flags() const
{ return m_parameterFlags; }
private:
QString m_defaultPath;
WizardPageList m_extensionPages;
QString m_selectedPlatform;
Core::FeatureSet m_requiredFeatures;
DialogParameterFlags m_parameterFlags;
};
class CORE_EXPORT BaseFileWizard : public IWizard
......@@ -221,5 +232,6 @@ QList<WizardClass*> createMultipleBaseFileWizardInstances(const QList<BaseFileWi
} // namespace Core
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::GeneratedFile::Attributes)
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::WizardDialogParameters::DialogParameterFlags)
#endif // BASEFILEWIZARD_H
......@@ -56,7 +56,8 @@ public:
};
Q_DECLARE_FLAGS(WizardKinds, WizardKind)
enum WizardFlag {
PlatformIndependent = 0x01
PlatformIndependent = 0x01,
ForceCapitalLetterForFileName = 0x02
};
Q_DECLARE_FLAGS(WizardFlags, WizardFlag)
......
......@@ -65,8 +65,6 @@
#include <QtPlugin>
#include <QMainWindow>
#include <QShortcut>
#include <QDir>
#include <QTemporaryFile>
using namespace TextEditor;
using namespace TextEditor::Internal;
......@@ -100,54 +98,6 @@ TextEditorPlugin *TextEditorPlugin::instance()
return m_instance;
}
static const char wizardCategoryC[] = "U.General";
static inline QString wizardDisplayCategory()
{
return TextEditorPlugin::tr("General");
}
// A wizard that quickly creates a scratch buffer
// based on a temporary file without prompting for a path.
class ScratchFileWizard : public Core::IWizard
{
public:
virtual WizardKind kind() const { return FileWizard; }
virtual QIcon icon() const { return QIcon(); }
virtual QString description() const
{ return TextEditorPlugin::tr("Creates a scratch buffer using a temporary file."); }
virtual QString displayName() const
{ return TextEditorPlugin::tr("Scratch Buffer"); }
virtual QString id() const
{ return QLatin1String("Z.ScratchFile"); }
virtual QString category() const
{ return QLatin1String(wizardCategoryC); }
virtual QString displayCategory() const
{ return wizardDisplayCategory(); }
virtual QString descriptionImage() const
{ return QString(); }
virtual Core::FeatureSet requiredFeatures() const
{ return Core::FeatureSet(); }
virtual WizardFlags flags() const
{ return Core::IWizard::PlatformIndependent; }
virtual void runWizard(const QString &path, QWidget *parent, const QString &platform);
};
void ScratchFileWizard::runWizard(const QString &, QWidget *, const QString &)
{
QString tempPattern = QDir::tempPath();
if (!tempPattern.endsWith(QLatin1Char('/')))
tempPattern += QLatin1Char('/');
tempPattern += QLatin1String("scratchXXXXXX.txt");
QTemporaryFile file(tempPattern);
file.setAutoRemove(false);
QTC_ASSERT(file.open(), return; );
file.close();
Core::EditorManager *em = Core::EditorManager::instance();
em->openEditor(file.fileName(), Core::Id(), Core::EditorManager::ModeSwitch);
}
// ExtensionSystem::PluginInterface
bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
......@@ -160,15 +110,14 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe
wizardParameters.setDescription(tr("Creates a text file. The default file extension is <tt>.txt</tt>. "
"You can specify a different extension as part of the filename."));
wizardParameters.setDisplayName(tr("Text File"));
wizardParameters.setCategory(QLatin1String(wizardCategoryC));
wizardParameters.setDisplayCategory(wizardDisplayCategory());
wizardParameters.setCategory(QLatin1String("U.General"));
wizardParameters.setDisplayCategory(tr("General"));
wizardParameters.setFlags(Core::IWizard::PlatformIndependent);
TextFileWizard *wizard = new TextFileWizard(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
QLatin1String("text$"),
wizardParameters);
// Add text file wizard
addAutoReleasedObject(wizard);
addAutoReleasedObject(new ScratchFileWizard);
m_settings = new TextEditorSettings(this);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment