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: Alessandro Portale <alessandro.portale@nokia.com>
This commit is contained in:
Thomas Hartmann
2012-03-19 16:12:51 +01:00
parent d86b2abc0b
commit bbd5d2ddfe
12 changed files with 97 additions and 58 deletions

View File

@@ -167,6 +167,9 @@ void BaseValidatingLineEdit::slotChanged(const QString &t)
emit validChanged(); emit validChanged();
} }
} }
bool block = blockSignals(true);
setText(fixInputString(t));
blockSignals(block);
} }
void BaseValidatingLineEdit::slotReturnPressed() void BaseValidatingLineEdit::slotReturnPressed()
@@ -180,4 +183,9 @@ void BaseValidatingLineEdit::triggerChanged()
slotChanged(text()); slotChanged(text());
} }
QString BaseValidatingLineEdit::fixInputString(const QString &string)
{
return string;
}
} // namespace Utils } // namespace Utils

View File

@@ -77,6 +77,7 @@ signals:
protected: protected:
virtual bool validate(const QString &value, QString *errorMessage) const = 0; virtual bool validate(const QString &value, QString *errorMessage) const = 0;
virtual QString fixInputString(const QString &string);
protected slots: protected slots:
// Custom behaviour can be added here. The base implementation must // Custom behaviour can be added here. The base implementation must

View File

@@ -68,7 +68,8 @@ static const QRegExp &windowsDeviceSubDirPattern()
// ----------- FileNameValidatingLineEdit // ----------- FileNameValidatingLineEdit
FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) : FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
BaseValidatingLineEdit(parent), BaseValidatingLineEdit(parent),
m_allowDirectories(false) m_allowDirectories(false),
m_forceFirstCapitalLetter(false)
{ {
} }
@@ -82,6 +83,16 @@ void FileNameValidatingLineEdit::setAllowDirectories(bool v)
m_allowDirectories = 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. */ /* Validate a file base name, check for forbidden characters/strings. */
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@@ -147,6 +158,17 @@ bool FileNameValidatingLineEdit::validate(const QString &value, QString *errorM
&& validateFileName(value, allowDirectories(), errorMessage); && 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, bool FileNameValidatingLineEdit::validateFileNameExtension(const QString &fileName,
const QStringList &requiredExtensions, const QStringList &requiredExtensions,

View File

@@ -42,6 +42,7 @@ class QTCREATOR_UTILS_EXPORT FileNameValidatingLineEdit : public BaseValidatingL
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool allowDirectories READ allowDirectories WRITE setAllowDirectories) Q_PROPERTY(bool allowDirectories READ allowDirectories WRITE setAllowDirectories)
Q_PROPERTY(QStringList requiredExtensions READ requiredExtensions WRITE setRequiredExtensions) Q_PROPERTY(QStringList requiredExtensions READ requiredExtensions WRITE setRequiredExtensions)
Q_PROPERTY(bool forceFirstCapitalLetter READ forceFirstCapitalLetter WRITE setForceFirstCapitalLetter)
public: public:
explicit FileNameValidatingLineEdit(QWidget *parent = 0); explicit FileNameValidatingLineEdit(QWidget *parent = 0);
@@ -61,6 +62,13 @@ public:
bool allowDirectories() const; bool allowDirectories() const;
void setAllowDirectories(bool v); 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. * Sets a requred extension. If the extension is empty no extension is required.
* Default is empty. * Default is empty.
@@ -70,10 +78,12 @@ public:
protected: protected:
virtual bool validate(const QString &value, QString *errorMessage) const; virtual bool validate(const QString &value, QString *errorMessage) const;
virtual QString fixInputString(const QString &string);
private: private:
bool m_allowDirectories; bool m_allowDirectories;
QStringList m_requiredExtensionList; QStringList m_requiredExtensionList;
bool m_forceFirstCapitalLetter;
}; };
} // namespace Utils } // namespace Utils

View File

@@ -75,6 +75,16 @@ QString FileWizardDialog::path() const
return m_filePage->path(); 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) void FileWizardDialog::setPath(const QString &path)
{ {
m_filePage->setPath(path); m_filePage->setPath(path);

View File

@@ -49,6 +49,9 @@ public:
QString fileName() const; QString fileName() const;
QString path() const; QString path() const;
bool forceFirstCapitalLetterForFileName() const;
void setForceFirstCapitalLetterForFileName(bool b);
public slots: public slots:
void setPath(const QString &path); void setPath(const QString &path);
void setFileName(const QString &name); void setFileName(const QString &name);

View File

@@ -50,6 +50,7 @@ struct FileWizardPagePrivate
FileWizardPagePrivate(); FileWizardPagePrivate();
Ui::WizardPage m_ui; Ui::WizardPage m_ui;
bool m_complete; bool m_complete;
bool m_forceFirstCapitalLetter;
}; };
FileWizardPagePrivate::FileWizardPagePrivate() : FileWizardPagePrivate::FileWizardPagePrivate() :
@@ -121,6 +122,16 @@ void FileWizardPage::setPathLabel(const QString &label)
d->m_ui.pathLabel->setText(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() void FileWizardPage::slotValidChanged()
{ {
const bool newComplete = d->m_ui.pathChooser->isValid() && d->m_ui.nameLineEdit->isValid(); const bool newComplete = d->m_ui.pathChooser->isValid() && d->m_ui.nameLineEdit->isValid();

View File

@@ -59,6 +59,9 @@ public:
void setFileNameLabel(const QString &label); void setFileNameLabel(const QString &label);
void setPathLabel(const QString &label); void setPathLabel(const QString &label);
bool forceFirstCapitalLetterForFileName() const;
void setForceFirstCapitalLetterForFileName(bool b);
// Validate a base name entry field (potentially containing extension) // Validate a base name entry field (potentially containing extension)
static bool validateBaseName(const QString &name, QString *errorMessage = 0); static bool validateBaseName(const QString &name, QString *errorMessage = 0);

View File

@@ -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 // Create dialog and run it. Ensure that the dialog is deleted when
// leaving the func, but not before the IFileWizardExtension::process // leaving the func, but not before the IFileWizardExtension::process
// has been called // has been called
WizardDialogParameters::DialogParameterFlags dialogParameterFlags;
if (flags().testFlag(ForceCapitalLetterForFileName))
dialogParameterFlags |= WizardDialogParameters::ForceCapitalLetterForFileName;
const QScopedPointer<QWizard> wizard(createWizardDialog(parent, WizardDialogParameters(path, const QScopedPointer<QWizard> wizard(createWizardDialog(parent, WizardDialogParameters(path,
allExtensionPages, allExtensionPages,
platform, platform,
requiredFeatures()))); requiredFeatures(),
dialogParameterFlags)));
QTC_ASSERT(!wizard.isNull(), return); QTC_ASSERT(!wizard.isNull(), return);
GeneratedFiles files; GeneratedFiles files;
@@ -811,6 +818,8 @@ QWizard *StandardFileWizard::createWizardDialog(QWidget *parent,
const WizardDialogParameters &wizardDialogParameters) const const WizardDialogParameters &wizardDialogParameters) const
{ {
Utils::FileWizardDialog *standardWizardDialog = new Utils::FileWizardDialog(parent); Utils::FileWizardDialog *standardWizardDialog = new Utils::FileWizardDialog(parent);
if (wizardDialogParameters.flags().testFlag(WizardDialogParameters::ForceCapitalLetterForFileName))
standardWizardDialog->setForceFirstCapitalLetterForFileName(true);
standardWizardDialog->setWindowTitle(tr("New %1").arg(displayName())); standardWizardDialog->setWindowTitle(tr("New %1").arg(displayName()));
setupWizard(standardWizardDialog); setupWizard(standardWizardDialog);
standardWizardDialog->setPath(wizardDialogParameters.defaultPath()); standardWizardDialog->setPath(wizardDialogParameters.defaultPath());

View File

@@ -113,12 +113,19 @@ class CORE_EXPORT WizardDialogParameters
public: public:
typedef QList<QWizardPage *> WizardPageList; typedef QList<QWizardPage *> WizardPageList;
enum DialogParameterEnum {
ForceCapitalLetterForFileName = 0x01
};
Q_DECLARE_FLAGS(DialogParameterFlags, DialogParameterEnum)
explicit WizardDialogParameters(const QString &defaultPath, const WizardPageList &extensionPages, 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_defaultPath(defaultPath),
m_extensionPages(extensionPages), m_extensionPages(extensionPages),
m_selectedPlatform(platform), m_selectedPlatform(platform),
m_requiredFeatures(requiredFeatures) {} m_requiredFeatures(requiredFeatures),
m_parameterFlags(flags) {}
QString defaultPath() const QString defaultPath() const
{ return m_defaultPath; } { return m_defaultPath; }
@@ -132,11 +139,15 @@ public:
Core::FeatureSet requiredFeatures() const Core::FeatureSet requiredFeatures() const
{ return m_requiredFeatures; } { return m_requiredFeatures; }
DialogParameterFlags flags() const
{ return m_parameterFlags; }
private: private:
QString m_defaultPath; QString m_defaultPath;
WizardPageList m_extensionPages; WizardPageList m_extensionPages;
QString m_selectedPlatform; QString m_selectedPlatform;
Core::FeatureSet m_requiredFeatures; Core::FeatureSet m_requiredFeatures;
DialogParameterFlags m_parameterFlags;
}; };
class CORE_EXPORT BaseFileWizard : public IWizard class CORE_EXPORT BaseFileWizard : public IWizard
@@ -221,5 +232,6 @@ QList<WizardClass*> createMultipleBaseFileWizardInstances(const QList<BaseFileWi
} // namespace Core } // namespace Core
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::GeneratedFile::Attributes) Q_DECLARE_OPERATORS_FOR_FLAGS(Core::GeneratedFile::Attributes)
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::WizardDialogParameters::DialogParameterFlags)
#endif // BASEFILEWIZARD_H #endif // BASEFILEWIZARD_H

View File

@@ -56,7 +56,8 @@ public:
}; };
Q_DECLARE_FLAGS(WizardKinds, WizardKind) Q_DECLARE_FLAGS(WizardKinds, WizardKind)
enum WizardFlag { enum WizardFlag {
PlatformIndependent = 0x01 PlatformIndependent = 0x01,
ForceCapitalLetterForFileName = 0x02
}; };
Q_DECLARE_FLAGS(WizardFlags, WizardFlag) Q_DECLARE_FLAGS(WizardFlags, WizardFlag)

View File

@@ -65,8 +65,6 @@
#include <QtPlugin> #include <QtPlugin>
#include <QMainWindow> #include <QMainWindow>
#include <QShortcut> #include <QShortcut>
#include <QDir>
#include <QTemporaryFile>
using namespace TextEditor; using namespace TextEditor;
using namespace TextEditor::Internal; using namespace TextEditor::Internal;
@@ -100,54 +98,6 @@ TextEditorPlugin *TextEditorPlugin::instance()
return m_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 // ExtensionSystem::PluginInterface
bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage) 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>. " 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.")); "You can specify a different extension as part of the filename."));
wizardParameters.setDisplayName(tr("Text File")); wizardParameters.setDisplayName(tr("Text File"));
wizardParameters.setCategory(QLatin1String(wizardCategoryC)); wizardParameters.setCategory(QLatin1String("U.General"));
wizardParameters.setDisplayCategory(wizardDisplayCategory()); wizardParameters.setDisplayCategory(tr("General"));
wizardParameters.setFlags(Core::IWizard::PlatformIndependent); wizardParameters.setFlags(Core::IWizard::PlatformIndependent);
TextFileWizard *wizard = new TextFileWizard(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT), TextFileWizard *wizard = new TextFileWizard(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
QLatin1String("text$"), QLatin1String("text$"),
wizardParameters); wizardParameters);
// Add text file wizard // Add text file wizard
addAutoReleasedObject(wizard); addAutoReleasedObject(wizard);
addAutoReleasedObject(new ScratchFileWizard);
m_settings = new TextEditorSettings(this); m_settings = new TextEditorSettings(this);