[qet] [4484] New Element Wizard : tree view only display directory

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4484
Author:   blacksun
Date:     2016-05-15 16:46:01 +0200 (Sun, 15 May 2016)
Log Message:
-----------
New Element Wizard : tree view only display directory

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementscollectionmodel.cpp
    trunk/sources/ElementsCollection/elementscollectionmodel.h
    trunk/sources/ElementsCollection/elementscollectionwidget.cpp
    trunk/sources/ElementsCollection/elementslocation.cpp
    trunk/sources/ElementsCollection/elementslocation.h
    trunk/sources/newelementwizard.cpp
    trunk/sources/newelementwizard.h

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-05-15 14:46:01 UTC (rev 4484)
@@ -61,10 +61,22 @@
 		parent_item = static_cast<ElementCollectionItem*>(parent.internalPointer());
 
 	ElementCollectionItem *child_item = parent_item->child(row);
-	if (child_item->isValid())
-		return createIndex(row, column, child_item);
-	else
+	if (child_item->isValid()) {
+		if (m_hide_element) {
+			if (child_item->isDir()) {
+				return createIndex(row, column, child_item);
+			}
+			else {
+				return QModelIndex();
+			}
+		}
+		else {
+			return createIndex(row, column, child_item);
+		}
+	}
+	else {
 		return QModelIndex();
+	}
 }
 
 /**
@@ -102,7 +114,21 @@
 	else
 		parent_item = static_cast<ElementCollectionItem*> (parent.internalPointer());
 
-	return parent_item->childCount();
+	if (m_hide_element) {
+		int count_ = 0;
+
+		for (int i = 0 ; i<parent_item->childCount() ; i++)
+		{
+			if (parent_item->child(i)->isDir()) {
+				count_ ++;
+			}
+		}
+
+		return count_;
+	}
+	else {
+		return parent_item->childCount();
+	}
 }
 
 /**
@@ -325,6 +351,62 @@
 }
 
 /**
+ * @brief ElementsCollectionModel::index
+ * @param location
+ * @return Return the index of the item represented by location.
+ * index can be no valid
+ */
+QModelIndex ElementsCollectionModel::index(const ElementsLocation &location) const
+{
+	if (!location.exist()) {
+		return QModelIndex();
+	}
+
+	QList <ElementCollectionItem *> child_list;
+
+	for (int i=0 ; i<m_root_item->childCount() ; i++) {
+		child_list.append(m_root_item->child(i));
+	}
+
+		foreach(ElementCollectionItem *eci, child_list) {
+
+			ElementCollectionItem *match_eci = nullptr;
+
+			if (eci->type() == FileElementCollectionItem::Type) {
+				FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *>(eci);
+				if (feci) {
+					if ( (location.isCommonCollection() && feci->isCommonCollection()) ||
+						 (location.isCustomCollection() && !feci->isCommonCollection()) ) {
+						match_eci = feci->itemAtPath(location.collectionPath(false));
+					}
+				}
+			}
+			else if (eci->type() == XmlProjectElementCollectionItem::Type) {
+				XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(eci);
+				if (xpeci) {
+					match_eci = xpeci->itemAtPath(location.collectionPath(false));
+				}
+			}
+
+			if (match_eci) {
+				return createIndex(match_eci->row(), 0, match_eci);
+			}
+		}
+
+		return QModelIndex();
+}
+
+/**
+ * @brief ElementsCollectionModel::hideElement
+ * Hide element.
+ * Only directory is provided by the model
+ */
+void ElementsCollectionModel::hideElement()
+{
+	m_hide_element = true;
+}
+
+/**
  * @brief ElementsCollectionModel::itemForProject
  * @param project
  * @return the root item of project @project, or nullptr if not found.

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.h
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-05-15 14:46:01 UTC (rev 4484)
@@ -19,6 +19,7 @@
 #define ELEMENTSCOLLECTIONMODEL_H
 
 #include <QAbstractItemModel>
+#include "elementslocation.h"
 
 class ElementCollectionItem;
 class QETProject;
@@ -58,6 +59,8 @@
 		bool addProject(QETProject *project);
 		bool removeProject(QETProject *project);
 		QList<QETProject *> project() const;
+		QModelIndex index(const ElementsLocation &location) const;
+		void hideElement();
 
 	private:
 		XmlProjectElementCollectionItem *itemForProject(QETProject *project);
@@ -71,6 +74,7 @@
 		ElementCollectionItem *m_root_item;
 		QList <QETProject *> m_project_list;
 		QModelIndex m_parent_at_drop;
+		bool m_hide_element = false;
 };
 
 #endif // ELEMENTSCOLLECTIONMODEL_H

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-15 14:46:01 UTC (rev 4484)
@@ -25,7 +25,6 @@
 #include "qetmessagebox.h"
 #include "elementscategoryeditor.h"
 #include "newelementwizard.h"
-#include "elementscategory.h"
 #include "xmlprojectelementcollectionitem.h"
 #include "qetproject.h"
 #include "qetelementeditor.h"
@@ -368,16 +367,18 @@
 {
 	ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu);
 
-	if (eci->type() != FileElementCollectionItem::Type) return;
+	if (eci->type() != FileElementCollectionItem::Type) {
+		return;
+	}
 
 	FileElementCollectionItem *feci = static_cast<FileElementCollectionItem*>(eci);
-	if(feci->isCommonCollection()) return;
+	if(feci->isCommonCollection()) {
+		return;
+	}
 
-	ElementsCollectionItem *category = QETApp::collectionItem(ElementsLocation(feci->collectionPath()), false);
-	ElementsCategory *selected_category = category -> toCategory();
-	if (!selected_category) return;
-
 	NewElementWizard elmt_wizard(this);
+	ElementsLocation loc(feci->collectionPath());
+	elmt_wizard.preselectedLocation(loc);
 	elmt_wizard.exec();
 }
 

Modified: trunk/sources/ElementsCollection/elementslocation.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.cpp	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/ElementsCollection/elementslocation.cpp	2016-05-15 14:46:01 UTC (rev 4484)
@@ -388,6 +388,24 @@
 }
 
 /**
+ * @brief ElementsLocation::isCommonCollection
+ * @return True if this location represent an item from the common collection
+ */
+bool ElementsLocation::isCommonCollection() const
+{
+	return fileSystemPath().startsWith(QETApp::commonElementsDirN());
+}
+
+/**
+ * @brief ElementsLocation::isCustomCollection
+ * @return True if this location represent an item from the custom collection
+ */
+bool ElementsLocation::isCustomCollection() const
+{
+	return fileSystemPath().startsWith(QETApp::customElementsDirN());
+}
+
+/**
  * @brief ElementsLocation::isProject
  * @return True if this location represent an item from a project.
  */

Modified: trunk/sources/ElementsCollection/elementslocation.h
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.h	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/ElementsCollection/elementslocation.h	2016-05-15 14:46:01 UTC (rev 4484)
@@ -61,6 +61,8 @@
 		bool isElement() const;
 		bool isDirectory() const;
 		bool isFileSystem() const;
+		bool isCommonCollection() const;
+		bool isCustomCollection() const;
 		bool isProject() const;
 		bool exist() const;
 		bool isWritable() const;

Modified: trunk/sources/newelementwizard.cpp
===================================================================
--- trunk/sources/newelementwizard.cpp	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/newelementwizard.cpp	2016-05-15 14:46:01 UTC (rev 4484)
@@ -55,10 +55,25 @@
 }
 
 /**
+ * @brief NewElementWizard::preselectedLocation
+ * Select item in the tree view represented by location,
+ * @param location
+ */
+void NewElementWizard::preselectedLocation(const ElementsLocation &location)
+{
+	QModelIndex index = m_model->index(location);
+	if (index.isValid()) {
+		m_tree_view->scrollTo(index);
+		m_tree_view->setCurrentIndex(index);
+	}
+}
+
+/**
  * @brief NewElementWizard::buildStep1
  * @return
  */
-QWizardPage *NewElementWizard::buildStep1() {
+QWizardPage *NewElementWizard::buildStep1()
+{
 	QWizardPage *page = new QWizardPage();
 	page -> setProperty("WizardState", Category);
 	page -> setTitle(tr("Étape 1/3 : Catégorie parente", "wizard page title"));
@@ -66,14 +81,17 @@
 	QVBoxLayout *layout = new QVBoxLayout();
 
 	m_tree_view = new QTreeView(this);
-	ElementsCollectionModel *model_ = new ElementsCollectionModel(m_tree_view);
-	model_->addCustomCollection();
-	m_tree_view->setModel(model_);
+
+	m_model = new ElementsCollectionModel(m_tree_view);
+	m_model->hideElement();
+	m_model->addCustomCollection();
+
+	m_tree_view->setModel(m_model);
 	m_tree_view->setHeaderHidden(true);
+	m_tree_view->setAnimated(true);
 	layout->addWidget(m_tree_view);
-
 	
-	page -> setLayout(layout);
+	page->setLayout(layout);
 	return(page);
 }
 

Modified: trunk/sources/newelementwizard.h
===================================================================
--- trunk/sources/newelementwizard.h	2016-05-14 17:10:03 UTC (rev 4483)
+++ trunk/sources/newelementwizard.h	2016-05-15 14:46:01 UTC (rev 4484)
@@ -24,6 +24,7 @@
 class NamesListWidget;
 class QFileNameEdit;
 class QTreeView;
+class ElementsCollectionModel;
 
 /**
 	This class provides a wizard dialog enabling users to to specify the basic
@@ -34,34 +35,38 @@
 	  - the filename the element should be saved to
 	  - localized names
 */
-class NewElementWizard : public QWizard {
+class NewElementWizard : public QWizard
+{
 	Q_OBJECT
 	
-	// constructors, destructor
+		// constructors, destructor
 	public:
-	NewElementWizard(QWidget * = 0, Qt::WindowFlags = 0);
-	virtual ~NewElementWizard();
+		NewElementWizard(QWidget * = 0, Qt::WindowFlags = 0);
+		virtual ~NewElementWizard();
+
+		void preselectedLocation(const ElementsLocation &location);
 	
 	private:
-	NewElementWizard(const NewElementWizard &);
+		NewElementWizard(const NewElementWizard &);
 	
-	// attributes
+		// attributes
 	private:
-	enum WizardState { Category, Filename, Names };
-	QFileNameEdit *m_qle_filename;
-	NamesListWidget *m_names_list;
-	QString m_chosen_file;
-	QTreeView *m_tree_view = nullptr;
-	ElementsLocation m_chosen_location;
+		enum WizardState { Category, Filename, Names };
+		QFileNameEdit *m_qle_filename;
+		NamesListWidget *m_names_list;
+		QString m_chosen_file;
+		QTreeView *m_tree_view = nullptr;
+		ElementsLocation m_chosen_location;
+		ElementsCollectionModel *m_model = nullptr;
 	
-	// methods
+		// methods
 	private:
-	QWizardPage *buildStep1();
-	QWizardPage *buildStep2();
-	QWizardPage *buildStep3();
-	bool validStep1();
-	bool validStep2();
-	bool validateCurrentPage();
-	void createNewElement();
+		QWizardPage *buildStep1();
+		QWizardPage *buildStep2();
+		QWizardPage *buildStep3();
+		bool validStep1();
+		bool validStep2();
+		bool validateCurrentPage();
+		void createNewElement();
 };
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/