PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/qdigidoc-0.4.1/crypto/TreeWidget.cpp

#
C++ | 162 lines | 125 code | 17 blank | 20 comment | 9 complexity | 5715c23db496398ca5bb3d10849e04b8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * QDigiDocCrypto
  3. *
  4. * Copyright (C) 2009-2010 Estonian Informatics Centre
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include "TreeWidget.h"
  22. #include "common/Common.h"
  23. #include "CryptoDoc.h"
  24. #include <QDesktopServices>
  25. #include <QFileDialog>
  26. #include <QHeaderView>
  27. #include <QKeyEvent>
  28. #include <QMimeData>
  29. #include <QUrl>
  30. TreeWidget::TreeWidget( QWidget *parent )
  31. : QTreeWidget( parent )
  32. {
  33. setColumnCount( 4 );
  34. header()->setStretchLastSection( false );
  35. header()->setResizeMode( 0, QHeaderView::Stretch );
  36. header()->setResizeMode( 1, QHeaderView::ResizeToContents );
  37. header()->setResizeMode( 2, QHeaderView::ResizeToContents );
  38. header()->setResizeMode( 3, QHeaderView::ResizeToContents );
  39. connect( this, SIGNAL(clicked(QModelIndex)), SLOT(clicked(QModelIndex)) );
  40. connect( this, SIGNAL(doubleClicked(QModelIndex)), SLOT(openFile(QModelIndex)) );
  41. }
  42. void TreeWidget::clicked( const QModelIndex &index )
  43. {
  44. switch( index.column() )
  45. {
  46. case 2:
  47. {
  48. QString filepath = QFileDialog::getSaveFileName( this,
  49. tr("Save file"), QString( "%1/%2" )
  50. .arg( QDesktopServices::storageLocation( QDesktopServices::DocumentsLocation ) )
  51. .arg( model()->index( index.row(), 0 ).data().toString() ) );
  52. if( !filepath.isEmpty() )
  53. Q_EMIT save( index.row(), filepath );
  54. break;
  55. }
  56. case 3: Q_EMIT remove( index.row() ); break;
  57. default: break;
  58. }
  59. }
  60. void TreeWidget::keyPressEvent( QKeyEvent *e )
  61. {
  62. QModelIndexList i = selectionModel()->selectedRows();
  63. if( hasFocus() && !i.isEmpty() && i[0].isValid() )
  64. {
  65. switch( e->key() )
  66. {
  67. case Qt::Key_Delete:
  68. if( !isColumnHidden( 3 ) )
  69. {
  70. Q_EMIT remove( i[0].row() );
  71. e->accept();
  72. }
  73. break;
  74. case Qt::Key_Return:
  75. if( !isColumnHidden( 2 ) )
  76. {
  77. openFile( i[0] );
  78. e->accept();
  79. }
  80. break;
  81. default: break;
  82. }
  83. }
  84. QTreeWidget::keyPressEvent( e );
  85. }
  86. QMimeData* TreeWidget::mimeData( const QList<QTreeWidgetItem*> items ) const
  87. {
  88. QList<QUrl> list;
  89. Q_FOREACH( QTreeWidgetItem *item, items )
  90. list << url( indexFromItem( item ) );
  91. QMimeData *data = new QMimeData();
  92. data->setUrls( list );
  93. return data;
  94. }
  95. QStringList TreeWidget::mimeTypes() const
  96. { return QStringList() << "text/uri-list"; }
  97. void TreeWidget::setContent( const QList<CDocument> &docs )
  98. {
  99. clear();
  100. Q_FOREACH( const CDocument &file, docs )
  101. {
  102. QTreeWidgetItem *i = new QTreeWidgetItem( this );
  103. if( file.path.isEmpty() )
  104. i->setFlags( Qt::NoItemFlags );
  105. else
  106. i->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
  107. i->setText( 0, file.filename );
  108. i->setData( 0, Qt::ToolTipRole, file.filename );
  109. i->setData( 0, Qt::UserRole, file.path );
  110. i->setText( 1, file.size );
  111. i->setData( 1, Qt::TextAlignmentRole, (int)Qt::AlignRight|Qt::AlignVCenter );
  112. i->setData( 2, Qt::DecorationRole, QPixmap(":/images/ico_save.png") );
  113. i->setData( 2, Qt::ToolTipRole, tr("Save") );
  114. i->setData( 2, Qt::SizeHintRole, QSize( 20, 20 ) );
  115. i->setData( 3, Qt::DecorationRole, QPixmap(":/images/ico_delete.png") );
  116. i->setData( 3, Qt::ToolTipRole, tr("Remove") );
  117. i->setData( 3, Qt::SizeHintRole, QSize( 20, 20 ) );
  118. addTopLevelItem( i );
  119. }
  120. }
  121. void TreeWidget::openFile( const QModelIndex &index )
  122. {
  123. QUrl u = url( index );
  124. #ifdef Q_OS_WIN32
  125. QList<QByteArray> exts = qgetenv( "PATHEXT" ).split(';');
  126. exts << ".PIF" << ".SCR";
  127. QFileInfo f( u.toLocalFile() );
  128. Q_FOREACH( const QByteArray &ext, exts )
  129. {
  130. if( QString( ext ).contains( f.suffix(), Qt::CaseInsensitive ) )
  131. return;
  132. }
  133. #endif
  134. QDesktopServices::openUrl( u );
  135. }
  136. Qt::DropActions TreeWidget::supportedDropActions() const
  137. { return Qt::CopyAction; }
  138. QUrl TreeWidget::url( const QModelIndex &index ) const
  139. {
  140. QModelIndex i = index.model()->index( index.row(), 0 );
  141. QString path = i.data( Qt::UserRole ).toString();
  142. if( path.isEmpty() )
  143. return path;
  144. return QUrl::fromLocalFile( path );
  145. }