ccRecentFiles.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <QAction>
  18. #include <QDir>
  19. #include <QFile>
  20. #include <QMenu>
  21. #include <QSettings>
  22. #include <QString>
  23. #include <QStringList>
  24. #include "ccRecentFiles.h"
  25. #include "mainwindow.h"
  26. QString ccRecentFiles::s_settingKey( "RecentFiles" );
  27. ccRecentFiles::ccRecentFiles( QWidget *parent ) :
  28. QObject( parent )
  29. {
  30. m_menu = new QMenu( tr("Open Recent..."), parent );
  31. m_actionClearMenu = new QAction( tr("Clear Menu"), this );
  32. connect( m_actionClearMenu, &QAction::triggered, this, [this]() {
  33. m_settings.remove( s_settingKey );
  34. updateMenu();
  35. });
  36. updateMenu();
  37. }
  38. QMenu *ccRecentFiles::menu()
  39. {
  40. return m_menu;
  41. }
  42. void ccRecentFiles::addFilePath( const QString &filePath )
  43. {
  44. QStringList list = m_settings.value( s_settingKey ).toStringList();
  45. list.removeAll( filePath );
  46. list.prepend( filePath );
  47. // only save the last ten files
  48. if ( list.count() > 10 )
  49. {
  50. list = list.mid( 0, 10 );
  51. }
  52. m_settings.setValue( s_settingKey, list );
  53. updateMenu();
  54. }
  55. void ccRecentFiles::updateMenu()
  56. {
  57. m_menu->clear();
  58. const QStringList recentList = listRecent();
  59. for ( const QString &recentFile : recentList )
  60. {
  61. QAction *recentAction = new QAction( contractFilePath( recentFile ), this );
  62. recentAction->setData( recentFile );
  63. connect( recentAction, &QAction::triggered, this, &ccRecentFiles::openFileFromAction );
  64. m_menu->addAction( recentAction );
  65. }
  66. if ( !m_menu->actions().isEmpty() )
  67. {
  68. m_menu->addSeparator();
  69. m_menu->addAction( m_actionClearMenu );
  70. }
  71. m_menu->setEnabled( !m_menu->actions().isEmpty() );
  72. }
  73. void ccRecentFiles::openFileFromAction()
  74. {
  75. QAction *action = qobject_cast<QAction *>( sender() );
  76. Q_ASSERT( action );
  77. QString fileName = action->data().toString();
  78. if ( !QFile::exists( fileName ) )
  79. {
  80. return;
  81. }
  82. QStringList fileListOfOne{ fileName };
  83. MainWindow::TheInstance()->addToDB( fileListOfOne );
  84. }
  85. QStringList ccRecentFiles::listRecent()
  86. {
  87. QStringList list = m_settings.value( s_settingKey ).toStringList();
  88. QStringList::iterator iter = list.begin();
  89. while ( iter != list.end() )
  90. {
  91. const QString filePath = *iter;
  92. if ( !QFile::exists( filePath ) )
  93. {
  94. iter = list.erase( iter );
  95. continue;
  96. }
  97. ++iter;
  98. }
  99. return list;
  100. }
  101. QString ccRecentFiles::contractFilePath( const QString &filePath )
  102. {
  103. QString homePath = QDir::toNativeSeparators( QDir::homePath() );
  104. QString newPath = QDir::toNativeSeparators( filePath );
  105. if ( newPath.startsWith( homePath ) )
  106. {
  107. return newPath.replace( 0, QDir::homePath().length(), '~' );
  108. }
  109. return filePath;
  110. }
  111. QString ccRecentFiles::expandFilePath( const QString &filePath )
  112. {
  113. QString newPath = QDir::toNativeSeparators( filePath );
  114. if ( newPath.startsWith( '~' ) )
  115. {
  116. QString homePath = QDir::toNativeSeparators( QDir::homePath() );
  117. return newPath.replace( 0, 1, homePath );
  118. }
  119. return filePath;
  120. }