From ec145f36f3e9107966fd690baf0fc70ae0de372a Mon Sep 17 00:00:00 2001
From: Pawel Faron <pawelfaron87@wp.pl>
Date: Tue, 16 Apr 2013 20:52:55 +0200
Subject: [PATCH] Added new filter into "Import Existing Project" and "Edit
 Files"

Extended classes:
- SelectableFilesModel
- SelectableFilesDialog
- FilesSelectionWizardPage

Task-number: QTCREATORBUG-8805
Change-Id: I0ff2f458fce2d7b0ceee24ace7dc2fafddc5ad5d
Reviewed-by: Petar Perisin <petar.perisin@gmail.com>
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
---
 .../filesselectionwizardpage.cpp              |  95 +++++++++----
 .../filesselectionwizardpage.h                |  21 ++-
 .../genericprojectconstants.h                 |   7 +-
 .../selectablefilesmodel.cpp                  | 125 +++++++++++++-----
 .../selectablefilesmodel.h                    |  40 +++++-
 5 files changed, 219 insertions(+), 69 deletions(-)

diff --git a/src/plugins/genericprojectmanager/filesselectionwizardpage.cpp b/src/plugins/genericprojectmanager/filesselectionwizardpage.cpp
index 9f8c7144739..be2dba58b78 100644
--- a/src/plugins/genericprojectmanager/filesselectionwizardpage.cpp
+++ b/src/plugins/genericprojectmanager/filesselectionwizardpage.cpp
@@ -48,22 +48,9 @@ FilesSelectionWizardPage::FilesSelectionWizardPage(GenericProjectWizardDialog *g
 {
     QVBoxLayout *layout = new QVBoxLayout(this);
 
-    QHBoxLayout *hbox = new QHBoxLayout;
-    m_filterLabel = new QLabel;
-    m_filterLabel->setText(tr("Hide files matching:"));
-    m_filterLabel->hide();
-    hbox->addWidget(m_filterLabel);
-    m_filterLineEdit = new QLineEdit;
-
-    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::FILEFILTER_SETTING),
-                                                          QLatin1String(Constants::FILEFILTER_DEFAULT)).toString();
-    m_filterLineEdit->setText(filter);
-    m_filterLineEdit->hide();
-    hbox->addWidget(m_filterLineEdit);
-    m_applyFilterButton = new QPushButton(tr("Apply Filter"), this);
-    m_applyFilterButton->hide();
-    hbox->addWidget(m_applyFilterButton);
-    layout->addLayout(hbox);
+    createShowFileFilterControls(layout);
+    createHideFileFilterControls(layout);
+    createApplyButton(layout);
 
     m_view = new QTreeView;
     m_view->setMinimumSize(500, 400);
@@ -74,6 +61,54 @@ FilesSelectionWizardPage::FilesSelectionWizardPage(GenericProjectWizardDialog *g
 
     layout->addWidget(m_view);
     layout->addWidget(m_label);
+}
+
+void FilesSelectionWizardPage::createHideFileFilterControls(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+    m_hideFilesFilterLabel = new QLabel;
+    m_hideFilesFilterLabel->setText(tr("Hide files matching:"));
+
+    m_hideFilesFilterLabel->hide();
+    hbox->addWidget(m_hideFilesFilterLabel);
+    m_hideFilesfilterLineEdit = new QLineEdit;
+
+    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::HIDE_FILE_FILTER_SETTING),
+                                                          QLatin1String(Constants::HIDE_FILE_FILTER_DEFAULT)).toString();
+    m_hideFilesfilterLineEdit->setText(filter);
+    m_hideFilesfilterLineEdit->hide();
+    hbox->addWidget(m_hideFilesfilterLineEdit);
+    layout->addLayout(hbox);
+}
+
+void FilesSelectionWizardPage::createShowFileFilterControls(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+    m_showFilesFilterLabel = new QLabel;
+    m_showFilesFilterLabel->setText(tr("Show files matching:"));
+    m_showFilesFilterLabel->hide();
+    hbox->addWidget(m_showFilesFilterLabel);
+    m_showFilesfilterLineEdit = new QLineEdit;
+
+    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::SHOW_FILE_FILTER_SETTING),
+                                                          QLatin1String(Constants::SHOW_FILE_FILTER_DEFAULT)).toString();
+    m_showFilesfilterLineEdit->setText(filter);
+    m_showFilesfilterLineEdit->hide();
+    hbox->addWidget(m_showFilesfilterLineEdit);
+    layout->addLayout(hbox);
+}
+
+void FilesSelectionWizardPage::createApplyButton(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+
+    QSpacerItem *horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
+    hbox->addItem(horizontalSpacer);
+
+    m_applyFilterButton = new QPushButton(tr("Apply Filter"), this);
+    m_applyFilterButton->hide();
+    hbox->addWidget(m_applyFilterButton);
+    layout->addLayout(hbox);
 
     connect(m_applyFilterButton, SIGNAL(clicked()), this, SLOT(applyFilter()));
 }
@@ -88,8 +123,13 @@ void FilesSelectionWizardPage::initializePage()
     connect(m_model, SIGNAL(parsingFinished()),
             this, SLOT(parsingFinished()));
     m_model->startParsing();
-    m_filterLabel->setVisible(false);
-    m_filterLineEdit->setVisible(false);
+
+    m_hideFilesFilterLabel->setVisible(false);
+    m_hideFilesfilterLineEdit->setVisible(false);
+
+    m_showFilesFilterLabel->setVisible(false);
+    m_showFilesfilterLineEdit->setVisible(false);
+
     m_applyFilterButton->setVisible(false);
     m_view->setVisible(false);
     m_label->setVisible(true);
@@ -110,8 +150,13 @@ void FilesSelectionWizardPage::parsingProgress(const QString &text)
 void FilesSelectionWizardPage::parsingFinished()
 {
     m_finished = true;
-    m_filterLabel->setVisible(true);
-    m_filterLineEdit->setVisible(true);
+
+    m_hideFilesFilterLabel->setVisible(true);
+    m_hideFilesfilterLineEdit->setVisible(true);
+
+    m_showFilesFilterLabel->setVisible(true);
+    m_showFilesfilterLineEdit->setVisible(true);
+
     m_applyFilterButton->setVisible(true);
     m_view->setVisible(true);
     m_label->setVisible(false);
@@ -139,9 +184,13 @@ QStringList FilesSelectionWizardPage::selectedFiles() const
 
 void FilesSelectionWizardPage::applyFilter()
 {
-    const QString filter = m_filterLineEdit->text();
-    Core::ICore::settings()->setValue(QLatin1String(Constants::FILEFILTER_SETTING), filter);
-    m_model->applyFilter(filter);
+    const QString showFilesFilter = m_showFilesfilterLineEdit->text();
+    Core::ICore::settings()->setValue(QLatin1String(Constants::SHOW_FILE_FILTER_SETTING), showFilesFilter);
+
+    const QString hideFilesFilter = m_hideFilesfilterLineEdit->text();
+    Core::ICore::settings()->setValue(QLatin1String(Constants::HIDE_FILE_FILTER_SETTING), hideFilesFilter);
+
+    m_model->applyFilter(showFilesFilter, hideFilesFilter);
 }
 
 } // namespace Internal
diff --git a/src/plugins/genericprojectmanager/filesselectionwizardpage.h b/src/plugins/genericprojectmanager/filesselectionwizardpage.h
index ad82f3360ca..80cd1beceec 100644
--- a/src/plugins/genericprojectmanager/filesselectionwizardpage.h
+++ b/src/plugins/genericprojectmanager/filesselectionwizardpage.h
@@ -31,8 +31,11 @@
 #define FILESSELECTIONWIZARDPAGE_H
 
 #include <QWizardPage>
-#include <QLabel>
-#include <QTreeView>
+
+class QVBoxLayout;
+class QLabel;
+class QTreeView;
+class QLineEdit;
 
 namespace GenericProjectManager {
 namespace Internal {
@@ -58,11 +61,21 @@ private slots:
     void parsingFinished();
 
 private:
+    void createHideFileFilterControls(QVBoxLayout *layout);
+    void createShowFileFilterControls(QVBoxLayout *layout);
+    void createApplyButton(QVBoxLayout *layout);
+
     GenericProjectWizardDialog *m_genericProjectWizardDialog;
     SelectableFilesModel *m_model;
-    QLabel *m_filterLabel;
-    QLineEdit *m_filterLineEdit;
+
+    QLabel *m_hideFilesFilterLabel;
+    QLineEdit *m_hideFilesfilterLineEdit;
+
+    QLabel *m_showFilesFilterLabel;
+    QLineEdit *m_showFilesfilterLineEdit;
+
     QPushButton *m_applyFilterButton;
+
     QTreeView *m_view;
     QLabel *m_label;
     bool m_finished;
diff --git a/src/plugins/genericprojectmanager/genericprojectconstants.h b/src/plugins/genericprojectmanager/genericprojectconstants.h
index f40016fcba8..54ea0bcfa88 100644
--- a/src/plugins/genericprojectmanager/genericprojectconstants.h
+++ b/src/plugins/genericprojectmanager/genericprojectconstants.h
@@ -47,8 +47,11 @@ const char CONFIG_MIMETYPE[]    = "application/vnd.nokia.qt.generic.config";
 // Project
 const char GENERICPROJECT_ID[]  = "GenericProjectManager.GenericProject";
 
-const char FILEFILTER_SETTING[] = "GenericProject/FileFilter";
-const char FILEFILTER_DEFAULT[] = "Makefile*; *.o; *.obj; *~; *.files; *.config; *.creator; *.user; *.includes";
+const char HIDE_FILE_FILTER_SETTING[] = "GenericProject/FileFilter";
+const char HIDE_FILE_FILTER_DEFAULT[] = "Makefile*; *.o; *.obj; *~; *.files; *.config; *.creator; *.user; *.includes";
+
+const char SHOW_FILE_FILTER_SETTING[] = "GenericProject/ShowFileFilter";
+const char SHOW_FILE_FILTER_DEFAULT[] = "*.c; *.cc; *.cpp; *.cp; *.cxx; *.c++; *.h; *.hh; *.hpp; *.hxx;";
 
 } // namespace Constants
 } // namespace GenericProjectManager
diff --git a/src/plugins/genericprojectmanager/selectablefilesmodel.cpp b/src/plugins/genericprojectmanager/selectablefilesmodel.cpp
index 7161739e260..6ce393d47ad 100644
--- a/src/plugins/genericprojectmanager/selectablefilesmodel.cpp
+++ b/src/plugins/genericprojectmanager/selectablefilesmodel.cpp
@@ -40,6 +40,7 @@
 #include <QLineEdit>
 #include <QPushButton>
 #include <QTreeView>
+#include <QDir>
 
 namespace GenericProjectManager {
 namespace Internal {
@@ -116,18 +117,27 @@ bool SelectableFilesModel::filter(Tree *t)
         return false;
     if (m_files.contains(t->fullPath))
         return false;
-    foreach (const Glob &g, m_filter) {
-        if (g.mode == Glob::EXACT) {
-            if (g.matchString == t->name)
-                return true;
-        } else if (g.mode == Glob::ENDSWITH) {
-            if (t->name.endsWith(g.matchString))
-                return true;
-        } else if (g.mode == Glob::REGEXP) {
-            if (g.matchRegexp.exactMatch(t->name))
-                return true;
+
+    bool showFilterMatch = false;
+
+    //First loop through show file filters and
+    //if any of them match, cotinue checking.
+    foreach (const Glob &g, m_showFilesFilter) {
+        if (g.isMatch(t->name)) {
+            showFilterMatch = true;
+            break;
         }
     }
+
+    //If none of the "show file" filters match just return
+    if (!showFilterMatch)
+        return true;
+
+    foreach (const Glob &g, m_hideFilesFilter) {
+        if (g.isMatch(t->name))
+            return true;
+    }
+
     return false;
 }
 
@@ -381,9 +391,10 @@ QList<Glob> SelectableFilesModel::parseFilter(const QString &filter)
     return result;
 }
 
-void SelectableFilesModel::applyFilter(const QString &filter)
+void SelectableFilesModel::applyFilter(const QString &showFilesfilter, const QString &hideFilesfilter)
 {
-    m_filter = parseFilter(filter);
+    m_showFilesFilter = parseFilter(showFilesfilter);
+    m_hideFilesFilter = parseFilter(hideFilesfilter);
     applyFilter(createIndex(0, 0, m_root));
 }
 
@@ -512,22 +523,9 @@ SelectableFilesDialog::SelectableFilesDialog(const QString &path, const QStringL
 
     m_view = new QTreeView(this);
 
-    QHBoxLayout *hbox = new QHBoxLayout;
-    m_filterLabel = new QLabel(this);
-    m_filterLabel->setText(tr("Hide files matching:"));
-    m_filterLabel->hide();
-    hbox->addWidget(m_filterLabel);
-    m_filterLineEdit = new QLineEdit(this);
-
-    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::FILEFILTER_SETTING),
-                                                          QLatin1String(Constants::FILEFILTER_DEFAULT)).toString();
-    m_filterLineEdit->setText(filter);
-    m_filterLineEdit->hide();
-    hbox->addWidget(m_filterLineEdit);
-    m_applyFilterButton = new QPushButton(tr("Apply Filter"), this);
-    m_applyFilterButton->hide();
-    hbox->addWidget(m_applyFilterButton);
-    layout->addLayout(hbox);
+    createShowFileFilterControls(layout);
+    createHideFileFilterControls(layout);
+    createApplyButton(layout);
 
     m_selectableFilesModel = new SelectableFilesModel(path, this);
     m_selectableFilesModel->setInitialMarkedFiles(files);
@@ -553,8 +551,6 @@ SelectableFilesDialog::SelectableFilesDialog(const QString &path, const QStringL
             this, SLOT(reject()));
     layout->addWidget(buttonBox);
 
-    connect(m_applyFilterButton, SIGNAL(clicked()), this, SLOT(applyFilter()));
-
     connect(m_selectableFilesModel, SIGNAL(parsingProgress(QString)),
             this, SLOT(parsingProgress(QString)));
     connect(m_selectableFilesModel, SIGNAL(parsingFinished()),
@@ -563,6 +559,55 @@ SelectableFilesDialog::SelectableFilesDialog(const QString &path, const QStringL
     m_selectableFilesModel->startParsing();
 }
 
+void SelectableFilesDialog::createHideFileFilterControls(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+    m_hideFilesFilterLabel = new QLabel;
+    m_hideFilesFilterLabel->setText(tr("Hide files matching:"));
+    m_hideFilesFilterLabel->hide();
+    hbox->addWidget(m_hideFilesFilterLabel);
+    m_hideFilesfilterLineEdit = new QLineEdit;
+
+    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::HIDE_FILE_FILTER_SETTING),
+                                                          QLatin1String(Constants::HIDE_FILE_FILTER_DEFAULT)).toString();
+    m_hideFilesfilterLineEdit->setText(filter);
+    m_hideFilesfilterLineEdit->hide();
+    hbox->addWidget(m_hideFilesfilterLineEdit);
+    layout->addLayout(hbox);
+}
+
+void SelectableFilesDialog::createShowFileFilterControls(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+    m_showFilesFilterLabel = new QLabel;
+    m_showFilesFilterLabel->setText(tr("Show files matching:"));
+    m_showFilesFilterLabel->hide();
+    hbox->addWidget(m_showFilesFilterLabel);
+    m_showFilesfilterLineEdit = new QLineEdit;
+
+    const QString filter = Core::ICore::settings()->value(QLatin1String(Constants::SHOW_FILE_FILTER_SETTING),
+                                                          QLatin1String(Constants::SHOW_FILE_FILTER_DEFAULT)).toString();
+    m_showFilesfilterLineEdit->setText(filter);
+    m_showFilesfilterLineEdit->hide();
+    hbox->addWidget(m_showFilesfilterLineEdit);
+    layout->addLayout(hbox);
+}
+
+void SelectableFilesDialog::createApplyButton(QVBoxLayout *layout)
+{
+    QHBoxLayout *hbox = new QHBoxLayout;
+
+    QSpacerItem *horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
+    hbox->addItem(horizontalSpacer);
+
+    m_applyFilterButton = new QPushButton(tr("Apply Filter"), this);
+    m_applyFilterButton->hide();
+    hbox->addWidget(m_applyFilterButton);
+    layout->addLayout(hbox);
+
+    connect(m_applyFilterButton, SIGNAL(clicked()), this, SLOT(applyFilter()));
+}
+
 SelectableFilesDialog::~SelectableFilesDialog()
 {
     m_selectableFilesModel->cancel();
@@ -576,8 +621,12 @@ void SelectableFilesDialog::parsingProgress(const QString &fileName)
 
 void SelectableFilesDialog::parsingFinished()
 {
-    m_filterLabel->show();
-    m_filterLineEdit->show();
+    m_hideFilesFilterLabel->show();
+    m_hideFilesfilterLineEdit->show();
+
+    m_showFilesFilterLabel->show();
+    m_showFilesfilterLineEdit->show();
+
     m_applyFilterButton->show();
     m_view->show();
     m_progressLabel->hide();
@@ -610,10 +659,16 @@ QStringList SelectableFilesDialog::selectedFiles() const
 
 void SelectableFilesDialog::applyFilter()
 {
-    const QString filter = m_filterLineEdit->text();
-    Core::ICore::settings()->setValue(QLatin1String(Constants::FILEFILTER_SETTING), filter);
-    m_selectableFilesModel->applyFilter(filter);
+    const QString showFilesFilter = m_showFilesfilterLineEdit->text();
+    Core::ICore::settings()->setValue(QLatin1String(Constants::SHOW_FILE_FILTER_SETTING), showFilesFilter);
+
+    const QString hideFilesFilter = m_hideFilesfilterLineEdit->text();
+    Core::ICore::settings()->setValue(QLatin1String(Constants::HIDE_FILE_FILTER_SETTING), hideFilesFilter);
+
+    m_selectableFilesModel->applyFilter(showFilesFilter, hideFilesFilter);
 }
 
 } // namespace Internal
 } // namespace GenericProjectManager
+
+
diff --git a/src/plugins/genericprojectmanager/selectablefilesmodel.h b/src/plugins/genericprojectmanager/selectablefilesmodel.h
index 7d1fafb126b..b2c37fcb892 100644
--- a/src/plugins/genericprojectmanager/selectablefilesmodel.h
+++ b/src/plugins/genericprojectmanager/selectablefilesmodel.h
@@ -34,11 +34,14 @@
 #include <QSet>
 #include <QFutureInterface>
 #include <QFutureWatcher>
-#include <QFileSystemModel>
 #include <QDialog>
 #include <QTreeView>
 #include <QLabel>
 
+class QLabel;
+class QTreeView;
+class QVBoxLayout;
+
 namespace GenericProjectManager {
 namespace Internal {
 
@@ -61,6 +64,21 @@ struct Glob
     Mode mode;
     QString matchString;
     mutable QRegExp matchRegexp;
+
+    bool isMatch(const QString &text) const
+    {
+        if (mode == Glob::EXACT) {
+            if (text == matchString)
+                return true;
+        } else if (mode == Glob::ENDSWITH) {
+            if (text.endsWith(matchString))
+                return true;
+        } else if (mode == Glob::REGEXP) {
+            if (matchRegexp.exactMatch(text))
+                return true;
+        }
+        return false;
+    }
 };
 
 class SelectableFilesModel : public QAbstractItemModel
@@ -90,7 +108,7 @@ public:
     void startParsing();
     void waitForFinished();
     void cancel();
-    void applyFilter(const QString &filter);
+    void applyFilter(const QString &selectFilesfilter, const QString &hideFilesfilter);
 
 signals:
     void parsingFinished();
@@ -120,7 +138,9 @@ private:
     Tree *m_rootForFuture;
     int m_futureCount;
     bool m_allFiles;
-    QList<Glob> m_filter;
+
+    QList<Glob> m_hideFilesFilter;
+    QList<Glob> m_showFilesFilter;
 };
 
 class SelectableFilesDialog : public QDialog
@@ -139,10 +159,19 @@ private slots:
 
 private:
     void smartExpand(const QModelIndex &index);
+    void createShowFileFilterControls(QVBoxLayout *layout);
+    void createHideFileFilterControls(QVBoxLayout *layout);
+    void createApplyButton(QVBoxLayout *layout);
     SelectableFilesModel *m_selectableFilesModel;
-    QLabel *m_filterLabel;
-    QLineEdit *m_filterLineEdit;
+
+    QLabel *m_hideFilesFilterLabel;
+    QLineEdit *m_hideFilesfilterLineEdit;
+
+    QLabel *m_showFilesFilterLabel;
+    QLineEdit *m_showFilesfilterLineEdit;
+
     QPushButton *m_applyFilterButton;
+
     QTreeView *m_view;
     QLabel *m_preservedFiles;
     QLabel *m_progressLabel;
@@ -152,3 +181,4 @@ private:
 } // namespace GenericProjectManager
 
 #endif // SELECTABLEFILESMODEL_H
+
-- 
GitLab