PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mks-svn4458/monkey/src/xupmanager/gui/CommandsEditorModel.cpp

#
C++ | 349 lines | 260 code | 71 blank | 18 comment | 52 complexity | d137a4ad8b470312f753c0a7ec0c5cd1 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-3.0, Zlib, AGPL-1.0
  1. /****************************************************************************
  2. Copyright (C) 2005 - 2011 Filipe AZEVEDO & The Monkey Studio Team
  3. http://monkeystudio.org licensing under the GNU GPL.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. ****************************************************************************/
  16. #include "CommandsEditorModel.h"
  17. #include "consolemanager/pCommand.h"
  18. #include <pMenuBar.h>
  19. #include <QApplication>
  20. #include <QStyle>
  21. #include <QDebug>
  22. #define CommandsEditorModelColumnCount 1
  23. CommandsEditorModel::CommandsEditorModel( QObject* parent )
  24. : QAbstractItemModel( parent )
  25. {
  26. mMenuBar = 0;
  27. }
  28. CommandsEditorModel::~CommandsEditorModel()
  29. {
  30. }
  31. int CommandsEditorModel::columnCount( const QModelIndex& parent ) const
  32. {
  33. Q_UNUSED( parent );
  34. return CommandsEditorModelColumnCount;
  35. }
  36. QVariant CommandsEditorModel::data( const QModelIndex& index, int role ) const
  37. {
  38. const bool isMenu = index.parent() == QModelIndex();
  39. const QString menu = this->menu( index );
  40. const pCommand command = this->command( index );
  41. const bool toBeDeleted = command.userData() == Qt::Checked;
  42. switch ( role ) {
  43. case Qt::CheckStateRole:
  44. return isMenu ? QVariant() : ( command.userData().isNull() ? Qt::Unchecked : command.userData().toInt() );
  45. case Qt::DecorationRole: {
  46. const QIcon icon = isMenu ? ( mMenuBar && !menu.isEmpty() ? mMenuBar->menu( menu )->icon() : QIcon() ) : QIcon();
  47. return icon.isNull() ? QVariant() : icon;
  48. }
  49. case Qt::DisplayRole:
  50. case Qt::ToolTipRole:
  51. return isMenu ? ( mMenuBar && !menu.isEmpty() ? mMenuBar->menu( menu )->title() : QVariant() ) : command.text();
  52. case Qt::ForegroundRole:
  53. return !isMenu && toBeDeleted ? QApplication::palette().brush( QPalette::Disabled, QPalette::WindowText ) : QVariant();
  54. case Qt::FontRole: {
  55. QFont font;
  56. font.setBold( isMenu );
  57. font.setStrikeOut( !isMenu && toBeDeleted );
  58. return font;
  59. }
  60. case Qt::EditRole:
  61. return QVariant::fromValue( command );
  62. case Qt::SizeHintRole: {
  63. const int height = QApplication::style()->pixelMetric( QStyle::PM_IndicatorHeight );
  64. return QSize( -1, height );
  65. }
  66. }
  67. return QVariant();
  68. }
  69. QModelIndex CommandsEditorModel::index( int row, int column, const QModelIndex& parent ) const
  70. {
  71. if ( row < 0 || column < 0 || column >= CommandsEditorModelColumnCount ) {
  72. return QModelIndex();
  73. }
  74. if ( parent == QModelIndex() ) {
  75. return row < mCommands.count() ? createIndex( row, column, -1 ) : QModelIndex();
  76. }
  77. const QString menu = this->menu( parent );
  78. if ( menu.isEmpty() || row >= mCommands.value( menu ).count() ) {
  79. return QModelIndex();
  80. }
  81. return createIndex( row, column, mMenusOrder[ menu ] );
  82. }
  83. QModelIndex CommandsEditorModel::parent( const QModelIndex& index ) const
  84. {
  85. return index.internalId() == -1 ? QModelIndex() : this->index( mMenusOrder.key( index.internalId() ) );
  86. }
  87. int CommandsEditorModel::rowCount( const QModelIndex& parent ) const
  88. {
  89. return parent == QModelIndex() ? mCommands.count() : mCommands.value( menu( parent ) ).count();
  90. }
  91. Qt::ItemFlags CommandsEditorModel::flags( const QModelIndex& index ) const
  92. {
  93. Qt::ItemFlags flags = QAbstractItemModel::flags( index );
  94. if ( index.isValid() && index.parent().isValid() ) {
  95. flags |= Qt::ItemIsUserCheckable;
  96. }
  97. return flags;
  98. }
  99. bool CommandsEditorModel::setData( const QModelIndex& index, const QVariant& value, int role )
  100. {
  101. if ( !index.isValid() || index.parent() == QModelIndex() ) {
  102. return false;
  103. }
  104. const QString menu = this->menu( index.parent() );
  105. if ( menu.isEmpty() ) {
  106. return false;
  107. }
  108. switch ( role ) {
  109. case Qt::CheckStateRole:
  110. mCommands[ menu ][ index.row() ].setUserData( value );
  111. break;
  112. case Qt::EditRole:
  113. mCommands[ menu ][ index.row() ] = value.value<pCommand>();
  114. break;
  115. default:
  116. return false;
  117. }
  118. emit dataChanged( index, index );
  119. return true;
  120. }
  121. QModelIndex CommandsEditorModel::index( const QString& menu ) const
  122. {
  123. if ( !mCommands.contains( menu ) ) {
  124. return QModelIndex();
  125. }
  126. return index( mMenusOrder[ menu ], 0 );
  127. }
  128. QModelIndex CommandsEditorModel::index( const pCommand& command, const QString& menu ) const
  129. {
  130. const int row = mCommands.value( menu ).indexOf( command );
  131. return row != -1 ? index( row, 0, index( menu ) ) : QModelIndex();
  132. }
  133. QString CommandsEditorModel::menu( const QModelIndex& index ) const
  134. {
  135. return index.parent() == QModelIndex() ? mMenusOrder.key( index.row() ) : QString::null;
  136. }
  137. pCommand CommandsEditorModel::command( const QModelIndex& index ) const
  138. {
  139. const QString menu = this->menu( index.parent() );
  140. const int count = mCommands.value( menu ).count();
  141. return menu.isEmpty() || index.row() >= count ? pCommand() : mCommands[ menu ][ index.row() ];
  142. }
  143. bool CommandsEditorModel::isEmpty() const
  144. {
  145. foreach ( const QString& menu, mCommands.keys() ) {
  146. if ( !mCommands[ menu ].isEmpty() ) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. void CommandsEditorModel::clear()
  153. {
  154. if ( isEmpty() ) {
  155. return;
  156. }
  157. emit layoutAboutToBeChanged();
  158. const QModelIndexList oldIndexes = persistentIndexList();
  159. QModelIndexList newIndexes;
  160. foreach ( const QModelIndex& index, oldIndexes ) {
  161. if ( index.parent() != QModelIndex() ) {
  162. newIndexes << QModelIndex();
  163. }
  164. else {
  165. newIndexes << index;
  166. }
  167. }
  168. foreach ( const QString& menu, mCommands.keys() ) {
  169. mCommands[ menu ].clear();
  170. }
  171. changePersistentIndexList( oldIndexes, newIndexes );
  172. emit layoutChanged();
  173. }
  174. void CommandsEditorModel::setCommands( const MenuCommandListMap& commands, pMenuBar* menuBar )
  175. {
  176. clear();
  177. mMenuBar = menuBar;
  178. if ( mCommands.isEmpty() ) {
  179. const QStringList menus = mMenuBar ? mMenuBar->rootMenusPath() : QStringList();
  180. const int count = menus.count();
  181. if ( count > 0 ) {
  182. beginInsertRows( QModelIndex(), 0, count -1 );
  183. int i = 0;
  184. foreach ( const QString& menu, menus ) {
  185. mCommands[ menu ] = pCommand::List();
  186. mMenusOrder[ menu ] = i;
  187. i++;
  188. }
  189. endInsertRows();
  190. }
  191. }
  192. else {
  193. emit dataChanged( index( 0, 0 ), index( rowCount() -1, columnCount() -1 ) );
  194. }
  195. foreach ( const QString& menu, commands.keys() ) {
  196. addCommands( menu, commands[ menu ] );
  197. }
  198. }
  199. MenuCommandListMap CommandsEditorModel::commands() const
  200. {
  201. return mCommands;
  202. }
  203. QModelIndex CommandsEditorModel::addCommand( const QModelIndex& menuIndex, const pCommand& command )
  204. {
  205. if ( !menuIndex.isValid() ) {
  206. return QModelIndex();
  207. }
  208. const int count = rowCount( menuIndex );
  209. beginInsertRows( menuIndex, count, count );
  210. mCommands[ menu( menuIndex ) ] << command;
  211. mCommands[ menu( menuIndex ) ].last().setUserData( Qt::Unchecked );
  212. endInsertRows();
  213. return index( count, 0, menuIndex );
  214. }
  215. void CommandsEditorModel::swapCommand( const QModelIndex& menuIndex, int fromCommand, int toCommand )
  216. {
  217. const QString menu = this->menu( menuIndex );
  218. if ( menu.isEmpty() || fromCommand < 0 || toCommand < 0 || fromCommand == toCommand ) {
  219. return;
  220. }
  221. pCommand::List& commands = mCommands[ menu ];
  222. const int count = commands.count();
  223. if ( fromCommand >= count || toCommand >= count ) {
  224. return;
  225. }
  226. emit layoutAboutToBeChanged();
  227. const QModelIndex from = index( fromCommand, 0, menuIndex );
  228. const QModelIndex to = index( toCommand, 0, menuIndex );
  229. const QModelIndexList oldIndexes = persistentIndexList();
  230. QModelIndexList newIndexes = persistentIndexList();
  231. const int fromIndex = oldIndexes.indexOf( from );
  232. const int toIndex = oldIndexes.indexOf( to );
  233. if ( fromIndex != -1 ) {
  234. newIndexes[ fromIndex ] = to;
  235. }
  236. if ( toIndex != -1 ) {
  237. newIndexes[ toIndex ] = from;
  238. }
  239. commands.swap( fromCommand, toCommand );
  240. changePersistentIndexList( oldIndexes, newIndexes );
  241. emit layoutChanged();
  242. }
  243. void CommandsEditorModel::revert()
  244. {
  245. QAbstractItemModel::revert();
  246. }
  247. bool CommandsEditorModel::submit()
  248. {
  249. const bool senderIsItemSelectionModel = sender() ? sender()->inherits( "QItemSelectionModel" ) : false;
  250. if ( senderIsItemSelectionModel ) {
  251. return false;
  252. }
  253. emit layoutAboutToBeChanged();
  254. foreach ( const QString& menu, mCommands.keys() ) {
  255. pCommand::List& commands = mCommands[ menu ];
  256. for ( int i = commands.count() -1; i >= 0; i-- ) {
  257. pCommand& command = commands[ i ];
  258. if ( command.userData().toInt() == Qt::Checked ) {
  259. commands.removeAt( i );
  260. }
  261. }
  262. }
  263. emit layoutChanged();
  264. return true;
  265. }
  266. void CommandsEditorModel::addCommands( const QString& menu, const pCommand::List& commands )
  267. {
  268. if ( commands.isEmpty() ) {
  269. return;
  270. }
  271. const int count = commands.count();
  272. beginInsertRows( index( menu ), 0, count -1 );
  273. mCommands[ menu ] = commands;
  274. endInsertRows();
  275. }