ccPluginInfoDlg.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: CloudCompare project #
  15. //# #
  16. //##########################################################################
  17. #include <QDebug>
  18. #include <QDir>
  19. #include <QSortFilterProxyModel>
  20. #include <QStandardItemModel>
  21. #include "ccPluginInfoDlg.h"
  22. #include "ccPluginManager.h"
  23. #include "ccStdPluginInterface.h"
  24. #include "ui_ccPluginInfoDlg.h"
  25. static QString sFormatReferenceList( const ccPluginInterface::ReferenceList &list )
  26. {
  27. const QString linkFormat( " <a href=\"%1\" style=\"text-decoration:none\">&#x1F517;</a>" );
  28. QString formattedText;
  29. int referenceNum = 1;
  30. for ( const ccPluginInterface::Reference &reference : list )
  31. {
  32. formattedText += QStringLiteral( "%1. " ).arg( QString::number( referenceNum++ ) );
  33. formattedText += reference.article;
  34. if ( !reference.url.isEmpty() )
  35. {
  36. formattedText += linkFormat.arg( reference.url );
  37. }
  38. formattedText += QStringLiteral( "<br/>" );
  39. }
  40. return formattedText;
  41. }
  42. static QString sFormatContactList( const ccPluginInterface::ContactList &list, const QString &pluginName )
  43. {
  44. const QString emailFormat( "&lt;<a href=\"mailto:%1?Subject=CloudCompare %2\">%1</a>&gt;" );
  45. QString formattedText;
  46. for ( const ccPluginInterface::Contact &contact : list )
  47. {
  48. formattedText += contact.name;
  49. if ( !contact.email.isEmpty() )
  50. {
  51. formattedText += emailFormat.arg( contact.email, pluginName );
  52. }
  53. formattedText += QStringLiteral( "<br/>" );
  54. }
  55. return formattedText;
  56. }
  57. namespace
  58. {
  59. class _Icons
  60. {
  61. public:
  62. static QIcon sGetIcon( CC_PLUGIN_TYPE inPluginType )
  63. {
  64. if ( sIconMap.empty() )
  65. {
  66. _init();
  67. }
  68. return sIconMap[inPluginType];
  69. }
  70. private:
  71. static void _init()
  72. {
  73. if ( !sIconMap.empty() )
  74. {
  75. return;
  76. }
  77. sIconMap[CC_STD_PLUGIN] = QIcon( ":/CC/pluginManager/images/std_plugin.png" );
  78. sIconMap[CC_GL_FILTER_PLUGIN] = QIcon( ":/CC/pluginManager/images/gl_plugin.png" );
  79. sIconMap[CC_IO_FILTER_PLUGIN] = QIcon( ":/CC/pluginManager/images/io_plugin.png" );
  80. }
  81. static QMap<CC_PLUGIN_TYPE, QIcon> sIconMap;
  82. };
  83. QMap<CC_PLUGIN_TYPE, QIcon> _Icons::sIconMap;
  84. }
  85. ccPluginInfoDlg::ccPluginInfoDlg( QWidget *parent ) :
  86. QDialog( parent )
  87. , m_UI( new Ui::ccPluginInfoDlg )
  88. , m_ProxyModel( new QSortFilterProxyModel( this ) )
  89. , m_ItemModel( new QStandardItemModel( this ) )
  90. {
  91. m_UI->setupUi( this );
  92. setWindowTitle( tr("About Plugins" ) );
  93. m_UI->mWarningLabel->setText( tr( "Enabling/disabling plugins will take effect next time you run %1" ).arg( QApplication::applicationName() ) );
  94. m_UI->mWarningLabel->setStyleSheet( QStringLiteral( "QLabel { background-color : #FFFF99; color : black; }" ) );
  95. m_UI->mWarningLabel->hide();
  96. m_UI->mSearchLineEdit->setStyleSheet( "QLineEdit, QLineEdit:focus { border: none; }" );
  97. m_UI->mSearchLineEdit->setAttribute( Qt::WA_MacShowFocusRect, false );
  98. m_ProxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
  99. m_ProxyModel->setSourceModel( m_ItemModel );
  100. m_UI->mPluginListView->setModel( m_ProxyModel );
  101. m_UI->mPluginListView->setFocus();
  102. connect( m_UI->mSearchLineEdit, &QLineEdit::textEdited, m_ProxyModel, &QSortFilterProxyModel::setFilterFixedString );
  103. connect( m_UI->mPluginListView->selectionModel(), &QItemSelectionModel::currentChanged,
  104. this, &ccPluginInfoDlg::selectionChanged );
  105. }
  106. ccPluginInfoDlg::~ccPluginInfoDlg()
  107. {
  108. delete m_UI;
  109. }
  110. void ccPluginInfoDlg::setPluginPaths( const QStringList &pluginPaths )
  111. {
  112. QString paths;
  113. for ( const QString &path : pluginPaths )
  114. {
  115. paths += QDir::toNativeSeparators( path );
  116. paths += QStringLiteral( "\n" );
  117. }
  118. m_UI->mPluginPathTextEdit->setText( paths );
  119. }
  120. void ccPluginInfoDlg::setPluginList( const QList<ccPluginInterface *> &pluginList )
  121. {
  122. m_ItemModel->clear();
  123. m_ItemModel->setRowCount( pluginList.count() );
  124. m_ItemModel->setColumnCount( 1 );
  125. int row = 0;
  126. for ( const ccPluginInterface *plugin : pluginList )
  127. {
  128. auto name = plugin->getName();
  129. auto tooltip = tr( "%1 Plugin" ).arg( plugin->getName() );
  130. if ( plugin->isCore() )
  131. {
  132. tooltip += tr( " (core)" );
  133. }
  134. else
  135. {
  136. name += " 👽";
  137. tooltip += tr( " (3rd Party)" );
  138. }
  139. QStandardItem *item = new QStandardItem( name );
  140. item->setCheckable( true );
  141. if ( ccPluginManager::Get().isEnabled( plugin ) )
  142. {
  143. item->setCheckState( Qt::Checked );
  144. }
  145. item->setData( QVariant::fromValue( plugin ), PLUGIN_PTR );
  146. item->setIcon( _Icons::sGetIcon( plugin->getType() ) );
  147. item->setToolTip( tooltip );
  148. m_ItemModel->setItem( row, 0, item );
  149. ++row;
  150. }
  151. if ( !pluginList.empty() )
  152. {
  153. m_ItemModel->sort( 0 );
  154. QModelIndex index = m_ItemModel->index( 0, 0 );
  155. m_UI->mPluginListView->setCurrentIndex( index );
  156. }
  157. connect( m_ItemModel, &QStandardItemModel::itemChanged, this, &ccPluginInfoDlg::itemChanged );
  158. }
  159. const ccPluginInterface *ccPluginInfoDlg::pluginFromItemData( const QStandardItem *item ) const
  160. {
  161. return item->data( PLUGIN_PTR ).value<const ccPluginInterface*>();;
  162. }
  163. void ccPluginInfoDlg::selectionChanged( const QModelIndex &current, const QModelIndex &previous )
  164. {
  165. Q_UNUSED( previous );
  166. auto sourceItem = m_ProxyModel->mapToSource( current );
  167. auto item = m_ItemModel->itemFromIndex( sourceItem );
  168. if ( item == nullptr )
  169. {
  170. // This happens if we are filtering and there are no results
  171. updatePluginInfo( nullptr );
  172. return;
  173. }
  174. auto plugin = pluginFromItemData( item );
  175. updatePluginInfo( plugin );
  176. }
  177. void ccPluginInfoDlg::itemChanged( QStandardItem *item )
  178. {
  179. bool checked = item->checkState() == Qt::Checked;
  180. auto plugin = pluginFromItemData( item );
  181. if ( plugin != nullptr )
  182. {
  183. ccPluginManager::Get().setPluginEnabled( plugin, checked );
  184. if ( m_UI->mWarningLabel->isHidden() )
  185. {
  186. ccLog::Warning( m_UI->mWarningLabel->text() );
  187. m_UI->mWarningLabel->show();
  188. }
  189. }
  190. }
  191. void ccPluginInfoDlg::updatePluginInfo( const ccPluginInterface *plugin )
  192. {
  193. if ( plugin == nullptr )
  194. {
  195. m_UI->mIcon->setPixmap( QPixmap() );
  196. m_UI->mNameLabel->setText( tr( "(No plugin selected)" ) );
  197. m_UI->mDescriptionTextEdit->clear();
  198. m_UI->mReferencesTextBrowser->clear();
  199. m_UI->mAuthorsTextBrowser->clear();
  200. m_UI->mMaintainerTextBrowser->clear();
  201. return;
  202. }
  203. const QSize iconSize( 64, 64 );
  204. QPixmap iconPixmap;
  205. if ( !plugin->getIcon().isNull() )
  206. {
  207. iconPixmap = plugin->getIcon().pixmap( iconSize );
  208. }
  209. switch ( plugin->getType() )
  210. {
  211. case CC_STD_PLUGIN:
  212. {
  213. if ( iconPixmap.isNull() )
  214. {
  215. iconPixmap = QPixmap( ":/CC/pluginManager/images/std_plugin.png" ).scaled( iconSize );
  216. }
  217. m_UI->mPluginTypeLabel->clear();
  218. break;
  219. }
  220. case CC_GL_FILTER_PLUGIN:
  221. {
  222. if ( iconPixmap.isNull() )
  223. {
  224. iconPixmap = QPixmap( ":/CC/pluginManager/images/gl_plugin.png" ).scaled( iconSize );
  225. }
  226. m_UI->mPluginTypeLabel->setText( tr( "GL Shader" ) );
  227. break;
  228. }
  229. case CC_IO_FILTER_PLUGIN:
  230. {
  231. if ( iconPixmap.isNull() )
  232. {
  233. iconPixmap = QPixmap( ":/CC/pluginManager/images/io_plugin.png" ).scaled( iconSize );
  234. }
  235. m_UI->mPluginTypeLabel->setText( tr( "I/O" ) );
  236. break;
  237. }
  238. }
  239. m_UI->mIcon->setPixmap( iconPixmap );
  240. m_UI->mNameLabel->setText( plugin->getName() );
  241. m_UI->mDescriptionTextEdit->setHtml( plugin->getDescription() );
  242. const QString referenceText = sFormatReferenceList( plugin->getReferences() );
  243. if ( !referenceText.isEmpty() )
  244. {
  245. m_UI->mReferencesTextBrowser->setHtml( referenceText );
  246. m_UI->mReferencesLabel->show();
  247. m_UI->mReferencesTextBrowser->show();
  248. }
  249. else
  250. {
  251. m_UI->mReferencesLabel->hide();
  252. m_UI->mReferencesTextBrowser->hide();
  253. m_UI->mReferencesTextBrowser->clear();
  254. }
  255. const QString authorsText = sFormatContactList( plugin->getAuthors(), plugin->getName() );
  256. if ( !authorsText.isEmpty() )
  257. {
  258. m_UI->mAuthorsTextBrowser->setHtml( authorsText );
  259. }
  260. else
  261. {
  262. m_UI->mAuthorsTextBrowser->clear();
  263. }
  264. const QString maintainersText = sFormatContactList( plugin->getMaintainers(), plugin->getName() );
  265. if ( !maintainersText.isEmpty() )
  266. {
  267. m_UI->mMaintainerTextBrowser->setHtml( maintainersText );
  268. }
  269. else
  270. {
  271. m_UI->mMaintainerTextBrowser->clear();
  272. }
  273. }