Commit ad68bb23 authored by hjk's avatar hjk
Browse files

pluginhowto: update example code

Still not fully compilable
parent d58f0e7c
......@@ -37,59 +37,62 @@
****************************************************************************/
#include "headerfilter.h"
#include <projectexplorer/projectexplorer.h>
#include <extensionsystem/pluginmanager.h>
#include <find/searchresultwindow.h>
#include <projectexplorer/iprojectmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/filesearch.h>
#include<QFutureWatcher>
#include<QLabel>
#include <find/searchresultwindow.h>
#include <texteditor/basetexteditor.h>
#include <utils/filesearch.h>
#include <QFutureWatcher>
#include <QLabel>
using namespace Core;
using namespace Utils;
struct HeaderFilterData
{
HeaderFilterData() : m_projectPlugin(0), m_searchResultWindow(0){}
QFutureWatcher<FileSearchResult> watcher;
class HeaderFilterPrivate
{
public:
HeaderFilterPrivate()
: m_projectPlugin(0), m_searchResultWindow(0)
{}
ProjectExplorer::ProjectExplorerPlugin* projectExplorer()
ProjectExplorer::ProjectExplorerPlugin *projectExplorer()
{
if(m_projectPlugin)
if (m_projectPlugin)
return m_projectPlugin;
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
m_projectPlugin = pm->getObject<ProjectExplorer::ProjectExplorerPlugin>();
return m_projectPlugin;
}
// Method to search and return the search window
Find::SearchResultWindow* searchResultWindow()
Find::SearchResultWindow *searchResultWindow()
{
if(m_searchResultWindow)
if (m_searchResultWindow)
return m_searchResultWindow;
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
m_searchResultWindow = pm->getObject<Find::SearchResultWindow>();
return m_searchResultWindow;
}
private:
ProjectExplorer::ProjectExplorerPlugin* m_projectPlugin;
Find::SearchResultWindow *m_searchResultWindow;
QFutureWatcher<FileSearchResult> watcher;
private:
ProjectExplorer::ProjectExplorerPlugin *m_projectPlugin;
Find::SearchResultWindow *m_searchResultWindow;
};
HeaderFilter::HeaderFilter()
{
d = new HeaderFilterData;
d = new HeaderFilterPrivate;
d->watcher.setPendingResultsLimit(1);
// displayResult(int) is called when every a new
......@@ -107,20 +110,26 @@ QString HeaderFilter::id() const
return "HeaderFilter";
}
QString HeaderFilter::name() const
QString HeaderFilter::displayName() const
{
return tr("Header Filter");
}
bool HeaderFilter::isEnabled() const
bool HeaderFilter::canCancel() const
{
QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
if(projects.count())
return true;
return false;
}
void HeaderFilter::cancel()
{
}
bool HeaderFilter::isEnabled() const
{
QList<ProjectExplorer::Project *> projects = d->projectExplorer()->session()->projects();
return !projects.isEmpty();
}
QKeySequence HeaderFilter::defaultShortcut() const
{
return QKeySequence();
......@@ -128,31 +137,30 @@ QKeySequence HeaderFilter::defaultShortcut() const
QWidget *HeaderFilter::createConfigWidget()
{
return (new QLabel("This is a header filter"));
return new QLabel("This is a header filter");
}
void HeaderFilter::findAll(const QString &text,QTextDocument::FindFlags findFlags)
{
void HeaderFilter::findAll(const QString &text, Find::FindFlags findFlags)
{
// Fetch a list of all open projects
QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
QList<ProjectExplorer::Project *> projects = d->projectExplorer()->session()->projects();
// Make a list of files in each project
QStringList files;
Q_FOREACH(ProjectExplorer::Project* project, projects)
files += project->files(ProjectExplorer::Project::AllFiles);
foreach (ProjectExplorer::Project *project, projects)
files += project->files(ProjectExplorer::Project::AllFiles);
// Remove duplicates
files.removeDuplicates();
//------------------------------------------------------------
// Begin searching
QString includeline = "#include <" + text + ">";
QString includeline = "#include <" + text + '>';
Find::SearchResult *result = d->searchResultWindow()->startNewSearch();
d->watcher.setFuture(QFuture<FileSearchResult>());
//When result gets activated it invokes the openEditor function
// When result gets activated it invokes the openEditor function
connect(result, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
......
......@@ -48,12 +48,13 @@ class SearchResultWindow;
struct SearchResultItem;
}
QT_BEGIN_NAMESPACE
class QKeySequence;
class QWidget;
QT_END_NAMESPACE
class HeaderFilterPrivate;
struct HeaderFilterData;
class HeaderFilter : public Find::IFindFilter
{
Q_OBJECT
......@@ -61,19 +62,22 @@ class HeaderFilter : public Find::IFindFilter
public:
HeaderFilter();
~HeaderFilter();
QString id() const;
QString name() const;
bool isEnabled() const;
QKeySequence defaultShortcut() const;
void findAll(const QString &txt,QTextDocument::FindFlags findFlags);
void findAll(const QString &text, Find::FindFlags findFlags);
QWidget *createConfigWidget();
QString displayName() const;
bool canCancel() const;
void cancel();
protected slots:
void displayResult(int index);
void openEditor(const Find::SearchResultItem &item);
private:
HeaderFilterData *d;
HeaderFilterPrivate *d;
};
......
QTC_SOURCE = C:/Work/QtCreator
QTC_BUILD = C:/Work/QtCreator/build
#QTC_SOURCE = C:/Work/QtCreator
#QTC_BUILD = C:/Work/QtCreator/build
QTC_SOURCE = ../../../..
QTC_BUILD = ../../../..
DEFINES += FIND_LIBRARY
TEMPLATE = lib
......
......@@ -47,20 +47,15 @@ CustomProjectPlugin::CustomProjectPlugin()
// Do nothing
}
CustomProjectPlugin::~CustomProjectPlugin()
{
// Do notning
}
void CustomProjectPlugin::extensionsInitialized()
{
// Do nothing
}
bool CustomProjectPlugin::initialize(const QStringList& args, QString *errMsg)
bool CustomProjectPlugin::initialize(const QStringList &args, QString *errorMessage)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
Q_UNUSED(errorMessage);
addAutoReleasedObject(new CustomProjectWizard);
......
......@@ -45,10 +45,9 @@ class CustomProjectPlugin : public ExtensionSystem::IPlugin
{
public:
CustomProjectPlugin();
~CustomProjectPlugin();
void extensionsInitialized();
bool initialize(const QStringList & arguments, QString * errorString);
bool initialize(const QStringList &arguments, QString *errorString);
void shutdown();
};
......
QTC_SOURCE = C:/Work/QtCreator
QTC_BUILD = C:/Work/QtCreator/build
#QTC_SOURCE = C:/Work/QtCreator
#QTC_BUILD = C:/Work/QtCreator/build
QTC_SOURCE = ../../../../..
QTC_BUILD = ../../../../..
TEMPLATE = lib
TARGET = CustomProject
IDE_SOURCE_TREE = $$QTC_SOURCE
......
......@@ -46,10 +46,6 @@ CustomProjectWizard::CustomProjectWizard()
{
}
CustomProjectWizard::~CustomProjectWizard()
{
}
Core::IWizard::Kind CustomProjectWizard::kind() const
{
return IWizard::ProjectWizard;
......@@ -65,9 +61,9 @@ QString CustomProjectWizard::description() const
return "A custom project";
}
QString CustomProjectWizard::name() const
QString CustomProjectWizard::displayName() const
{
return "CustomProject";
return tr("CustomProject");
}
QString CustomProjectWizard::category() const
......@@ -80,10 +76,9 @@ QString CustomProjectWizard::trCategory() const
return tr("FooCompanyInc");
}
QStringList CustomProjectWizard::runWizard(const QString &path, QWidget *parent)
void CustomProjectWizard::runWizard(const QString &path, QWidget *parent)
{
Q_UNUSED(path);
Q_UNUSED(parent);
QMessageBox::information(parent, "Custom Wizard Dialog", "Hi there!");
return QStringList();
}
......@@ -45,15 +45,14 @@ class CustomProjectWizard : public Core::IWizard
{
public:
CustomProjectWizard();
~CustomProjectWizard();
Core::IWizard::Kind kind() const;
QIcon icon() const;
QString description() const;
QString name() const;
QString displayName() const;
QString category() const;
QString trCategory() const;
QStringList runWizard(const QString &path, QWidget *parent);
void runWizard(const QString &path, QWidget *parent);
};
#endif // CUSTOMPROJECTWIZARD_H
......@@ -38,7 +38,9 @@
#include "itemmodelwizardplugin.h"
#include "modelclasswizard.h"
#include <QApplication>
#include <QIcon>
#include <QtPlugin>
#include <QStringList>
......@@ -57,10 +59,10 @@ void ItemModelWizardPlugin::extensionsInitialized()
// Do nothing
}
bool ItemModelWizardPlugin::initialize(const QStringList& args, QString *errMsg)
bool ItemModelWizardPlugin::initialize(const QStringList &args, QString *errorMessage)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
Q_UNUSED(errorMessage);
Core::BaseFileWizardParameters params;
params.setKind(Core::IWizard::ClassWizard);
params.setIcon(qApp->windowIcon());
......
......@@ -48,7 +48,7 @@ public:
~ItemModelWizardPlugin();
void extensionsInitialized();
bool initialize(const QStringList & arguments, QString * errorString);
bool initialize(const QStringList &arguments, QString *errorString);
void shutdown();
};
......
QTC_SOURCE = C:/Work/QtCreator
QTC_BUILD = C:/Work/QtCreator/build
#QTC_SOURCE = C:/Work/QtCreator
#QTC_BUILD = C:/Work/QtCreator/build
QTC_SOURCE = ../../../../..
QTC_BUILD = ../../../../..
TEMPLATE = lib
TARGET = ItemModelWizard
IDE_SOURCE_TREE = $$QTC_SOURCE
......
......@@ -39,32 +39,27 @@
#include "modelclasswizard.h"
#include "modelnamepage.h"
#include <QFile>
#include <QFileInfo>
#include <cppeditor/cppeditor.h>
#include <cppeditor/cppeditorconstants.h>
#include <coreplugin/basefilewizard.h>
ModelClassWizard::ModelClassWizard(const Core::BaseFileWizardParameters &parameters,QObject *parent)
: Core::BaseFileWizard(parameters, parent)
{
}
#include <QFile>
#include <QFileInfo>
ModelClassWizard::~ModelClassWizard()
ModelClassWizard::ModelClassWizard(const Core::BaseFileWizardParameters &parameters, QObject *parent)
: Core::BaseFileWizard(parameters, parent)
{
}
QWizard * ModelClassWizard::createWizardDialog(
QWidget *parent,
const QString &defaultPath,
const WizardPageList &extensionPages) const
QWizard *ModelClassWizard::createWizardDialog(QWidget *parent,
const QString &defaultPath, const WizardPageList &extensionPages) const
{
// Create a wizard
QWizard* wizard = new QWizard(parent);
wizard->setWindowTitle("Model Class Wizard");
QWizard *wizard = new QWizard(parent);
wizard->setWindowTitle(tr("Model Class Wizard"));
// Make our page as first page
ModelNamePage* page = new ModelNamePage(wizard);
ModelNamePage *page = new ModelNamePage(wizard);
int pageId = wizard->addPage(page);
wizard->setProperty("_PageId_", pageId);
page->setPath(defaultPath);
......@@ -75,32 +70,29 @@ QWizard * ModelClassWizard::createWizardDialog(
return wizard;
}
QString ModelClassWizard::readFile(const QString& fileName, const QMap<QString,QString>&
replacementMap) const
QString ModelClassWizard::readFile(const QString& fileName,
const QMap<QString,QString> & replacementMap) const
{
QFile file(fileName);
file.open(QFile::ReadOnly);
QString retStr = file.readAll();
QString result = file.readAll();
QMap<QString,QString>::const_iterator it = replacementMap.begin();
QMap<QString,QString>::const_iterator end = replacementMap.end();
while(it != end)
{
retStr.replace(it.key(), it.value());
++it;
}
return retStr;
for (; it != end; ++it)
result.replace(it.key(), it.value());
return result;
}
Core::GeneratedFiles ModelClassWizard::generateFiles(
const QWizard *w,QString *errorMessage) const
Core::GeneratedFiles ModelClassWizard::generateFiles
(const QWizard *wizard, QString *errorMessage) const
{
Q_UNUSED(errorMessage);
Core::GeneratedFiles ret;
int pageId = w->property("_PageId_").toInt();
ModelNamePage* page = qobject_cast<ModelNamePage*>(w->page(pageId));
int pageId = wizard->property("_PageId_").toInt();
ModelNamePage *page = qobject_cast<ModelNamePage*>(wizard->page(pageId));
if(!page)
if (!page)
return ret;
ModelClassParameters params = page->parameters();
QMap<QString,QString> replacementMap;
......@@ -110,28 +102,25 @@ Core::GeneratedFiles ModelClassWizard::generateFiles(
replacementMap["{{CLASS_NAME}}"] = params.className;
replacementMap["{{CLASS_HEADER}}"] = QFileInfo(params.headerFile).fileName();
Core::GeneratedFile headerFile(params.path + "/" + params.headerFile);
Core::GeneratedFile headerFile(params.path + '/' + params.headerFile);
headerFile.setEditorKind(CppEditor::Constants::CPPEDITOR_KIND);
Core::GeneratedFile sourceFile(params.path + "/" + params.sourceFile);
Core::GeneratedFile sourceFile(params.path + '/' + params.sourceFile);
sourceFile.setEditorKind(CppEditor::Constants::CPPEDITOR_KIND);
if(params.baseClass == "QAbstractItemModel")
{
headerFile.setContents(readFile(":/CustomProject/ItemModelHeader", replacementMap) );
sourceFile.setContents(readFile(":/CustomProject/ItemModelSource", replacementMap) );
if (params.baseClass == "QAbstractItemModel") {
headerFile.setContents(readFile(":/CustomProject/ItemModelHeader", replacementMap));
sourceFile.setContents(readFile(":/CustomProject/ItemModelSource", replacementMap));
}
else if(params.baseClass == "QAbstractTableModel")
{
headerFile.setContents(readFile(":/CustomProject/TableModelHeader", replacementMap) );
sourceFile.setContents(readFile(":/CustomProject/TableModelSource", replacementMap) );
else if (params.baseClass == "QAbstractTableModel") {
headerFile.setContents(readFile(":/CustomProject/TableModelHeader", replacementMap));
sourceFile.setContents(readFile(":/CustomProject/TableModelSource", replacementMap));
}
else if(params.baseClass == "QAbstractListModel")
{
headerFile.setContents(readFile(":/CustomProject/ListModelHeader", replacementMap) );
sourceFile.setContents(readFile(":/CustomProject/ListModelSource", replacementMap) );
else if (params.baseClass == "QAbstractListModel") {
headerFile.setContents(readFile(":/CustomProject/ListModelHeader", replacementMap));
sourceFile.setContents(readFile(":/CustomProject/ListModelSource", replacementMap));
}
ret << headerFile << sourceFile;
......
......@@ -41,6 +41,7 @@
#include <coreplugin/basefilewizard.h>
#include <QMap>
class ModelClassWizard : public Core::BaseFileWizard
{
......@@ -48,17 +49,15 @@ class ModelClassWizard : public Core::BaseFileWizard
public:
explicit ModelClassWizard(const Core::BaseFileWizardParameters &parameters, QObject *parent = 0);
~ModelClassWizard();
QWizard *createWizardDialog(QWidget *parent,
const QString &defaultPath,
const WizardPageList &extensionPages) const;
QWizard *createWizardDialog(QWidget *parent, const QString &defaultPath,
const WizardPageList &extensionPages) const;
Core::GeneratedFiles generateFiles(const QWizard *w, QString *errorMessage) const;
private:
QString readFile(const QString& fileName,
const QMap<QString,QString>& replacementMap) const;
QString readFile(const QString &fileName,
const QMap<QString, QString> &replacementMap) const;
};
#endif // MODELCLASSWIZARD_H
......@@ -37,30 +37,24 @@
****************************************************************************/
#include "modelnamepage.h"
#include "ui_modelnamepage.h"
ModelNamePage::ModelNamePage(QWidget *parent) :
QWizardPage(parent)
ModelNamePage::ModelNamePage(QWidget *parent)
: QWizardPage(parent)
{
setTitle("Enter model class information");
setSubTitle("The header and source file names will be derived from the class name");
ui.setupUi(this);
}
ModelNamePage::~ModelNamePage()
{
}
void ModelNamePage::setPath(const QString& path)
void ModelNamePage::setPath(const QString &path)
{
this->path = path;
}
void ModelNamePage::on_txtModelClass_textEdited(const QString& txt)
void ModelNamePage::on_txtModelClass_textEdited(const QString &text)
{
ui.txtHeaderFile->setText(txt + ".h");
ui.txtImplFile->setText(txt + ".cpp");
ui.txtHeaderFile->setText(text + ".h");
ui.txtImplFile->setText(text + ".cpp");
}
ModelClassParameters ModelNamePage::parameters() const
......
......@@ -39,30 +39,31 @@
#ifndef MODELNAMEPAGE_H
#define MODELNAMEPAGE_H
#include "ui_modelnamepage.h"
#include <QWizardPage>
#include "ui_ModelNamePage.h"
struct ModelClassParameters
{
QString className;
QString headerFile;
QString sourceFile;
QString baseClass;
QString path;
};
{
QString className;
QString headerFile;
QString sourceFile;
QString baseClass;
QString path;
};
class ModelNamePage : public QWizardPage
class ModelNamePage : public QWizardPage
{
Q_OBJECT
public:
ModelNamePage(QWidget *parent = 0);
~ModelNamePage();
void setPath(const QString& path);
void setPath(const QString &path);
ModelClassParameters parameters() const;
private slots:
void on_txtModelClass_textEdited(const QString& txt);