2007-12-01 10:47:15 +00:00
/*
2017-01-20 10:55:49 +00:00
Copyright 2006 - 2017 The QElectroTech Team
2007-12-01 10:47:15 +00:00
This file is part of QElectroTech .
QElectroTech is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 2 of the License , or
( at your option ) any later version .
QElectroTech is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with QElectroTech . If not , see < http : //www.gnu.org/licenses/>.
*/
2007-01-30 22:32:21 +00:00
# include "elementspanel.h"
2007-09-21 13:22:18 +00:00
# include "qetapp.h"
2009-04-03 19:30:25 +00:00
# include "qetproject.h"
# include "diagram.h"
2009-05-01 14:41:33 +00:00
# include "qeticons.h"
2012-01-08 17:04:34 +00:00
# include "templatescollection.h"
2009-04-03 19:30:25 +00:00
/*
Lorsque le flag ENABLE_PANEL_DND_CHECKS est defini , le panel d ' elements
effectue des verifications lors des drag ' n drop d ' elements et categories .
Par exemple , il verifie qu ' une categorie cible est accessible en ecriture
avant d ' y autoriser le drop d ' un element .
Supprimer ce flag permet de tester le comportement des fonctions de gestion
des items ( copy , move , etc . ) .
*/
# define ENABLE_PANEL_DND_CHECKS
2006-10-27 15:47:22 +00:00
/**
Constructeur
@ param parent Le QWidget parent du panel d ' appareils
*/
2009-04-03 19:30:25 +00:00
ElementsPanel : : ElementsPanel ( QWidget * parent ) :
2012-02-06 21:21:43 +00:00
GenericPanel ( parent ) ,
2011-03-20 03:13:32 +00:00
first_reload_ ( true )
2009-04-03 19:30:25 +00:00
{
2006-10-27 15:47:22 +00:00
// selection unique
setSelectionMode ( QAbstractItemView : : SingleSelection ) ;
2006-10-28 19:39:43 +00:00
setColumnCount ( 1 ) ;
2010-11-15 00:35:02 +00:00
setExpandsOnDoubleClick ( true ) ;
2012-04-28 16:45:13 +00:00
setMouseTracking ( true ) ;
2006-10-27 15:47:22 +00:00
// drag'n drop autorise
setDragEnabled ( true ) ;
2009-04-03 19:30:25 +00:00
setAcceptDrops ( true ) ;
setDropIndicatorShown ( true ) ;
2010-11-14 20:11:57 +00:00
setAutoExpandDelay ( 1000 ) ;
2006-10-27 15:47:22 +00:00
2006-11-11 18:25:42 +00:00
// force du noir sur une alternance de blanc (comme le schema) et de gris
// clair, avec du blanc sur bleu pas trop fonce pour la selection
2006-10-27 15:47:22 +00:00
QPalette qp = palette ( ) ;
2007-02-01 01:07:26 +00:00
qp . setColor ( QPalette : : Text , Qt : : black ) ;
qp . setColor ( QPalette : : Base , Qt : : white ) ;
qp . setColor ( QPalette : : AlternateBase , QColor ( " #e8e8e8 " ) ) ;
qp . setColor ( QPalette : : Highlight , QColor ( " #678db2 " ) ) ;
2014-06-23 04:01:11 +00:00
qp . setColor ( QPalette : : HighlightedText , Qt : : black ) ;
2006-10-27 15:47:22 +00:00
setPalette ( qp ) ;
2007-06-30 17:41:07 +00:00
2015-11-14 10:44:24 +00:00
// we handle double click on items ourselves
connect ( this , & ElementsPanel : : itemDoubleClicked , this , & ElementsPanel : : slot_doubleClick ) ;
connect ( this , & GenericPanel : : firstActivated , [ this ] ( ) { QTimer : : singleShot ( 250 , this , SLOT ( reload ( ) ) ) ; } ) ;
connect ( this , & ElementsPanel : : panelContentChanged , this , & ElementsPanel : : panelContentChange ) ;
2012-02-06 21:21:43 +00:00
2015-11-14 10:44:24 +00:00
//Emit a signal instead au manage is own context menu
2007-10-28 16:03:18 +00:00
setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2006-10-27 15:47:22 +00:00
}
2007-04-12 03:13:13 +00:00
/**
Destructeur
*/
ElementsPanel : : ~ ElementsPanel ( ) {
}
2006-10-27 15:47:22 +00:00
/**
Gere le debut des drag ' n drop
@ param supportedActions Les actions supportees
2009-04-03 19:30:25 +00:00
*/
2009-11-22 16:12:22 +00:00
void ElementsPanel : : startDrag ( Qt : : DropActions supportedActions ) {
Q_UNUSED ( supportedActions ) ;
2009-04-03 19:30:25 +00:00
2012-02-06 21:21:43 +00:00
TitleBlockTemplateLocation tbt_location = selectedTemplateLocation ( ) ;
2012-01-22 10:40:37 +00:00
if ( tbt_location . isValid ( ) ) {
startTitleBlockTemplateDrag ( tbt_location ) ;
return ;
}
}
/**
Handle the dragging of a title block template
@ param location Location of the dragged template .
*/
void ElementsPanel : : startTitleBlockTemplateDrag ( const TitleBlockTemplateLocation & location ) {
QString location_string = location . toString ( ) ;
2018-07-30 15:24:29 +00:00
QMimeData * mime_data = new QMimeData ( ) ;
2012-01-22 10:40:37 +00:00
mime_data - > setText ( location_string ) ;
2015-03-02 20:14:56 +00:00
mime_data - > setData ( " application/x-qet-titleblock-uri " , location_string . toLatin1 ( ) ) ;
2012-01-22 10:40:37 +00:00
2018-07-30 15:24:29 +00:00
QDrag * drag = new QDrag ( this ) ;
2012-01-22 10:40:37 +00:00
drag - > setMimeData ( mime_data ) ;
drag - > setPixmap ( QET : : Icons : : TitleBlock . pixmap ( 22 , 16 ) ) ;
drag - > start ( Qt : : CopyAction ) ;
}
2012-02-12 15:33:04 +00:00
/**
Ensure the filter is applied again after the panel content has changed .
*/
void ElementsPanel : : panelContentChange ( ) {
if ( ! filter_ . isEmpty ( ) ) {
filter ( filter_ ) ;
}
}
2009-04-03 19:30:25 +00:00
/**
Methode permettant d ' ajouter un projet au panel d ' elements .
@ param qtwi_parent QTreeWidgetItem parent sous lequel sera insere le projet
@ param project Projet a inserer dans le panel d ' elements
@ return Le QTreeWidgetItem insere le plus haut
*/
2018-04-05 18:49:28 +00:00
QTreeWidgetItem * ElementsPanel : : addProject ( QETProject * project , QTreeWidgetItem * parent_item , PanelOptions options )
{
Q_UNUSED ( parent_item )
Q_UNUSED ( options )
2012-02-12 17:17:44 +00:00
bool first_add = ( first_reload_ | | ! projects_to_display_ . contains ( project ) ) ;
2012-02-06 21:21:43 +00:00
// create the QTreeWidgetItem representing the project
2017-08-05 02:06:59 +00:00
QTreeWidgetItem * qtwi_project = GenericPanel : : addProject ( project , nullptr , GenericPanel : : All ) ;
2012-01-08 17:04:34 +00:00
// the project will be inserted right before the common tb templates collection
2012-02-06 21:21:43 +00:00
invisibleRootItem ( ) - > insertChild (
indexOfTopLevelItem ( common_tbt_collection_item_ ) ,
qtwi_project
2009-04-03 19:30:25 +00:00
) ;
2012-02-12 17:17:44 +00:00
if ( first_add ) qtwi_project - > setExpanded ( true ) ;
2012-02-12 16:47:37 +00:00
if ( TitleBlockTemplatesCollection * tbt_collection = project - > embeddedTitleBlockTemplatesCollection ( ) ) {
if ( QTreeWidgetItem * tbt_collection_qtwi = itemForTemplatesCollection ( tbt_collection ) ) {
2012-02-12 17:17:44 +00:00
if ( first_add ) tbt_collection_qtwi - > setExpanded ( true ) ;
2012-02-12 16:47:37 +00:00
}
}
2015-03-02 20:14:56 +00:00
qtwi_project - > setStatusTip ( 0 , tr ( " Double-cliquez pour réduire ou développer ce projet " , " Status tip " ) ) ;
2012-04-28 16:45:13 +00:00
2009-04-03 19:30:25 +00:00
return ( qtwi_project ) ;
2006-10-27 15:47:22 +00:00
}
2006-10-28 19:39:43 +00:00
2012-05-03 05:28:28 +00:00
QTreeWidgetItem * ElementsPanel : : updateTemplatesCollectionItem ( QTreeWidgetItem * tbt_collection_qtwi , TitleBlockTemplatesCollection * tbt_collection , PanelOptions options , bool freshly_created ) {
QTreeWidgetItem * tbtc_qtwi = GenericPanel : : updateTemplatesCollectionItem ( tbt_collection_qtwi , tbt_collection , options , freshly_created ) ;
if ( tbt_collection & & tbt_collection - > parentProject ( ) ) {
2015-03-02 20:14:56 +00:00
tbtc_qtwi - > setText ( 0 , tr ( " Cartouches embarqués " ) ) ;
tbtc_qtwi - > setStatusTip ( 0 , tr ( " Double-cliquez pour réduire ou développer cette collection de cartouches embarquée " , " Status tip " ) ) ;
2012-05-03 05:28:28 +00:00
}
return ( tbtc_qtwi ) ;
}
2012-02-06 21:21:43 +00:00
QTreeWidgetItem * ElementsPanel : : updateTemplateItem ( QTreeWidgetItem * tb_template_qtwi , const TitleBlockTemplateLocation & tb_template , PanelOptions options , bool freshly_created ) {
QTreeWidgetItem * item = GenericPanel : : updateTemplateItem ( tb_template_qtwi , tb_template , options , freshly_created ) ;
item - > setStatusTip (
0 ,
tr (
2015-03-21 10:43:39 +00:00
" Glissez-déposez ce modèle de cartouche sur un folio pour l'y appliquer. " ,
2012-04-28 16:45:13 +00:00
" Status tip displayed when selecting a title block template "
2012-02-06 21:21:43 +00:00
)
) ;
return ( item ) ;
}
2014-10-16 20:35:32 +00:00
/**
@ return true if \ a item matches the filter , false otherwise
*/
2018-07-19 14:14:31 +00:00
bool ElementsPanel : : matchesFilter ( const QTreeWidgetItem * item , const QString & filter ) const {
2014-10-16 20:35:32 +00:00
if ( ! item ) return ( false ) ;
// no filter => we consider the item matches
if ( filter . isEmpty ( ) ) return ( true ) ;
bool item_matches = item - > text ( 0 ) . contains ( filter , Qt : : CaseInsensitive ) ;
return ( item_matches ) ;
}
2007-02-26 22:42:46 +00:00
/**
2014-01-23 19:59:04 +00:00
* @ brief ElementsPanel : : reload
* Reload the elements tree
* @ param reload_collections true for read all collections since their sources ( files , projects . . . )
*/
2009-04-03 19:30:25 +00:00
void ElementsPanel : : reload ( bool reload_collections ) {
2016-05-27 12:44:24 +00:00
Q_UNUSED ( reload_collections ) ;
2009-04-03 19:30:25 +00:00
2012-01-08 17:04:34 +00:00
QIcon system_icon ( " :/ico/16x16/qet.png " ) ;
QIcon user_icon ( " :/ico/16x16/go-home.png " ) ;
// load the common title block templates collection
TitleBlockTemplatesCollection * common_tbt_collection = QETApp : : commonTitleBlockTemplatesCollection ( ) ;
2012-02-06 21:21:43 +00:00
common_tbt_collection_item_ = addTemplatesCollection ( common_tbt_collection , invisibleRootItem ( ) ) ;
common_tbt_collection_item_ - > setIcon ( 0 , system_icon ) ;
2015-03-02 20:14:56 +00:00
common_tbt_collection_item_ - > setStatusTip ( 0 , tr ( " Double-cliquez pour réduire ou développer la collection de cartouches QElectroTech " , " Status tip " ) ) ;
common_tbt_collection_item_ - > setWhatsThis ( 0 , tr ( " Ceci est la collection de cartouches fournie avec QElectroTech. Installée en tant que composant système, vous ne pouvez normalement pas la personnaliser. " , " \" What's this \" tip " ) ) ;
2012-01-08 17:04:34 +00:00
if ( first_reload_ ) common_tbt_collection_item_ - > setExpanded ( true ) ;
// load the custom title block templates collection
TitleBlockTemplatesCollection * custom_tbt_collection = QETApp : : customTitleBlockTemplatesCollection ( ) ;
2012-02-06 21:21:43 +00:00
custom_tbt_collection_item_ = addTemplatesCollection ( custom_tbt_collection , invisibleRootItem ( ) ) ;
custom_tbt_collection_item_ - > setIcon ( 0 , user_icon ) ;
2015-03-02 20:14:56 +00:00
custom_tbt_collection_item_ - > setStatusTip ( 0 , tr ( " Double-cliquez pour réduire ou développer votre collection personnelle de cartouches " , " Status tip " ) ) ;
custom_tbt_collection_item_ - > setWhatsThis ( 0 , tr ( " Ceci est votre collection personnelle de cartouches -- utilisez-la pour créer, stocker et éditer vos propres cartouches. " , " \" What's this \" tip " ) ) ;
2012-01-08 17:04:34 +00:00
if ( first_reload_ ) custom_tbt_collection_item_ - > setExpanded ( true ) ;
2012-02-06 21:21:43 +00:00
// add projects
2017-02-05 16:18:50 +00:00
foreach ( QETProject * project , projects_to_display_ . values ( ) ) {
2012-02-06 21:21:43 +00:00
addProject ( project ) ;
2007-12-20 19:18:31 +00:00
}
2012-01-08 17:04:34 +00:00
// the first time, expand the first level of collections
if ( first_reload_ ) first_reload_ = false ;
2007-02-26 22:42:46 +00:00
}
2007-06-30 17:41:07 +00:00
2007-10-10 17:50:26 +00:00
/**
2009-04-03 19:30:25 +00:00
Gere le double - clic sur un element .
Si un double - clic sur un projet est effectue , le signal requestForProject
est emis .
Si un double - clic sur un schema est effectue , le signal requestForDiagram
est emis .
@ param qtwi
2007-10-10 17:50:26 +00:00
*/
2009-04-03 19:30:25 +00:00
void ElementsPanel : : slot_doubleClick ( QTreeWidgetItem * qtwi , int ) {
2012-02-06 21:21:43 +00:00
int qtwi_type = qtwi - > type ( ) ;
if ( qtwi_type = = QET : : Project ) {
2018-07-30 15:24:29 +00:00
QETProject * project = valueForItem < QETProject * > ( qtwi ) ;
2009-04-03 19:30:25 +00:00
emit ( requestForProject ( project ) ) ;
2012-02-06 21:21:43 +00:00
} else if ( qtwi_type = = QET : : Diagram ) {
2018-07-30 15:24:29 +00:00
Diagram * diagram = valueForItem < Diagram * > ( qtwi ) ;
2009-04-03 19:30:25 +00:00
emit ( requestForDiagram ( diagram ) ) ;
2012-02-06 21:21:43 +00:00
} else if ( qtwi_type = = QET : : TitleBlockTemplate ) {
TitleBlockTemplateLocation tbt = valueForItem < TitleBlockTemplateLocation > ( qtwi ) ;
emit ( requestForTitleBlockTemplate ( tbt ) ) ;
2009-04-03 19:30:25 +00:00
}
}
2013-02-03 19:22:58 +00:00
/**
@ param qtwi a QTreeWidgetItem
@ return the directory path of the object represented by \ a qtwi
*/
QString ElementsPanel : : dirPathForItem ( QTreeWidgetItem * item ) {
QString file_path = filePathForItem ( item ) ;
if ( ! file_path . isEmpty ( ) ) {
QFileInfo path_info ( file_path ) ;
if ( path_info . isDir ( ) ) {
return ( file_path ) ;
}
else {
return ( path_info . canonicalPath ( ) ) ;
}
}
return ( QString ( ) ) ;
}
/**
@ param qtwi a QTreeWidgetItem
@ return the filepath of the object represented by \ a qtwi
*/
QString ElementsPanel : : filePathForItem ( QTreeWidgetItem * item ) {
if ( ! item ) return ( QString ( ) ) ;
2016-05-27 12:44:24 +00:00
TitleBlockTemplateLocation tbt_location = templateLocationForItem ( item ) ;
TitleBlockTemplatesCollection * tbt_collection = tbt_location . parentCollection ( ) ;
if ( tbt_collection & & tbt_collection - > hasFilePath ( ) ) {
return ( tbt_collection - > filePath ( ) ) ;
2013-02-03 19:22:58 +00:00
}
else {
2016-05-27 12:44:24 +00:00
QETProject * project = projectForItem ( item ) ;
if ( project ) {
return ( project - > filePath ( ) ) ;
2013-02-03 19:22:58 +00:00
}
}
return ( QString ( ) ) ;
}
2010-12-24 21:00:11 +00:00
/**
2012-02-06 21:21:43 +00:00
Hide items that do not match the provided string , ensure others are visible
along with their parent hierarchy . When ending the filtering , restore the tree
as it was before the filtering ( except the current item ) and scroll to the
currently selected item .
@ param m String to be matched
@ param filtering whether to begin / apply / end the filtering
@ see QET : : Filtering
2007-10-10 17:50:26 +00:00
*/
2012-02-06 21:21:43 +00:00
void ElementsPanel : : filter ( const QString & m , QET : : Filtering filtering ) {
QList < QTreeWidgetItem * > items = findItems ( " * " , Qt : : MatchRecursive | Qt : : MatchWildcard ) ;
const int expanded_role = 42 ; // magic number? So you consider Douglas Adams wrote about magic?
2007-06-30 17:41:07 +00:00
2012-02-06 21:21:43 +00:00
if ( filtering = = QET : : BeginFilter ) {
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * item , items ) {
2012-02-06 21:21:43 +00:00
item - > setData ( 0 , expanded_role , item - > isExpanded ( ) ) ;
}
}
2009-04-03 19:30:25 +00:00
2012-02-06 21:21:43 +00:00
if ( filtering ! = QET : : EndFilter ) {
2012-02-12 15:33:04 +00:00
filter_ = m ;
applyCurrentFilter ( items ) ;
2012-02-06 21:21:43 +00:00
} else { // filtering == QET::EndFilter
2012-02-12 15:33:04 +00:00
filter_ = QString ( ) ;
2012-02-06 21:21:43 +00:00
QTreeWidgetItem * current_item = currentItem ( ) ;
// restore the tree as it was before the filtering
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * qtwi , items ) {
2012-02-06 21:21:43 +00:00
qtwi - > setHidden ( false ) ;
qtwi - > setExpanded ( qtwi - > data ( 0 , expanded_role ) . toBool ( ) ) ;
}
// avoid hiding the currently selected item
if ( current_item ) {
ensureHierarchyIsVisible ( QList < QTreeWidgetItem * > ( ) < < current_item ) ;
scrollToItem ( current_item ) ;
}
2007-06-30 17:41:07 +00:00
}
}
2007-08-28 21:17:11 +00:00
2009-04-03 19:30:25 +00:00
/**
Rajoute un projet au panel d ' elements
@ param project Projet ouvert a rajouter au panel
*/
void ElementsPanel : : projectWasOpened ( QETProject * project ) {
2012-02-06 21:21:43 +00:00
addProject ( project ) ;
2012-02-12 17:17:44 +00:00
projects_to_display_ < < project ;
2012-02-12 16:47:37 +00:00
emit ( panelContentChanged ( ) ) ;
2007-08-28 21:17:11 +00:00
}
2007-10-10 17:50:26 +00:00
/**
2009-04-03 19:30:25 +00:00
Enleve un projet du panel d ' elements
@ param project Projet a enlever du panel
2007-10-10 17:50:26 +00:00
*/
2009-04-03 19:30:25 +00:00
void ElementsPanel : : projectWasClosed ( QETProject * project ) {
2012-02-06 21:21:43 +00:00
if ( QTreeWidgetItem * item_to_remove = itemForProject ( project ) ) {
GenericPanel : : deleteItem ( item_to_remove ) ;
2009-04-03 19:30:25 +00:00
projects_to_display_ . remove ( project ) ;
}
2012-02-12 16:47:37 +00:00
emit ( panelContentChanged ( ) ) ;
2007-08-28 21:17:11 +00:00
}
2014-10-16 20:35:32 +00:00
/**
Build filter list for multiple filter
*/
void ElementsPanel : : buildFilterList ( ) {
if ( filter_ . isEmpty ( ) ) return ;
filter_list_ = filter_ . split ( ' + ' ) ;
/*
qDebug ( ) < < " ******************* " ;
2017-02-05 16:18:50 +00:00
foreach ( QString filter , filter_list_ ) {
2014-10-16 20:35:32 +00:00
filter = filter . trimmed ( ) ;
qDebug ( ) < < filter ;
}
*/
}
2012-02-12 15:33:04 +00:00
/**
Apply the current filter to a given item .
*/
void ElementsPanel : : applyCurrentFilter ( const QList < QTreeWidgetItem * > & items ) {
if ( filter_ . isEmpty ( ) ) return ;
2014-10-16 20:35:32 +00:00
buildFilterList ( ) ;
2012-02-12 15:33:04 +00:00
QList < QTreeWidgetItem * > matching_items ;
2014-10-16 20:35:32 +00:00
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * item , items ) {
2014-10-16 20:35:32 +00:00
bool item_matches = true ;
2017-02-05 16:18:50 +00:00
foreach ( QString filter , filter_list_ ) {
2014-10-16 20:35:32 +00:00
filter = filter . trimmed ( ) ;
if ( ! filter . isEmpty ( ) ) {
item_matches & = matchesFilter ( item , filter ) ;
}
}
2012-02-12 15:33:04 +00:00
if ( item_matches ) matching_items < < item ;
item - > setHidden ( ! item_matches ) ;
}
ensureHierarchyIsVisible ( matching_items ) ;
}
2009-05-19 19:00:37 +00:00
/**
2009-11-22 16:12:22 +00:00
@ param items une liste de QTreeWidgetItem pour lesquels il faut s ' assurer
que eux et leurs parents sont visibles
2009-05-19 19:00:37 +00:00
*/
2012-02-12 15:33:04 +00:00
void ElementsPanel : : ensureHierarchyIsVisible ( const QList < QTreeWidgetItem * > & items ) {
2009-05-19 19:00:37 +00:00
// remonte l'arborescence pour lister les categories contenant les elements filtres
QSet < QTreeWidgetItem * > parent_items ;
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * item , items ) {
2009-05-19 19:00:37 +00:00
for ( QTreeWidgetItem * parent_qtwi = item - > parent ( ) ; parent_qtwi ; parent_qtwi = parent_qtwi - > parent ( ) ) {
parent_items < < parent_qtwi ;
}
}
// etend les parents
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * parent_qtwi , parent_items ) {
2009-05-19 19:00:37 +00:00
if ( ! parent_qtwi - > isExpanded ( ) ) parent_qtwi - > setExpanded ( true ) ;
}
// affiche les parents
2017-02-05 16:18:50 +00:00
foreach ( QTreeWidgetItem * parent_qtwi , parent_items ) {
2009-05-19 19:00:37 +00:00
if ( parent_qtwi - > isHidden ( ) ) parent_qtwi - > setHidden ( false ) ;
}
}