Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
recentfilemenu.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program 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
14 GNU General Public License for more details.
15 
16 */
17 
18 
19 #include <QSettings>
20 #include <QVariant>
21 #include <QDebug>
22 
23 #include "recentfilemenu.h"
24 
25 RecentFileMenu::RecentFileMenu( QString title, QWidget *parent ) :
26 QMenu( title, parent )
27 {
28  mClearSeparator = new QAction( this );
29  mClearSeparator->setSeparator( true );
30 
31  mClearAction = new QAction( tr("Clear"), this ); // share the same translation
32 }
33 
34 RecentFileMenu::~RecentFileMenu()
35 {
36  delete mClearSeparator;
37  delete mClearAction;
38 }
39 
40 void RecentFileMenu::clear()
41 {
42  for( QString filename : mRecentFiles )
43  {
44  removeRecentFile( filename );
45  }
46  removeAction( mClearSeparator );
47  removeAction( mClearAction );
48  mRecentFiles.clear();
49  mRecentActions.clear();
50 }
51 
52 void RecentFileMenu::setRecentFiles( QStringList filenames )
53 {
54  clear();
55  for( QString filename : filenames )
56  {
57  if ( filename != "" ) {
58  addRecentFile( filename );
59  }
60  }
61 }
62 
63 bool RecentFileMenu::loadFromDisk()
64 {
65  QSettings settings( PENCIL2D, PENCIL2D );
66  QVariant recent = settings.value( "RecentFiles" );
67  if ( recent.isNull() )
68  {
69  return false;
70  }
71  QList<QString> recentFileList = recent.toStringList();
72  setRecentFiles( recentFileList );
73  return true;
74 }
75 
76 bool RecentFileMenu::saveToDisk()
77 {
78  QSettings settings( PENCIL2D, PENCIL2D );
79  settings.setValue( "RecentFiles", QVariant( mRecentFiles ) );
80  return true;
81 }
82 
83 void RecentFileMenu::addRecentFile( QString filename )
84 {
85  if ( mRecentFiles.contains( filename ) )
86  {
87  removeRecentFile( filename );
88  }
89 
90  while ( mRecentFiles.size() >= MAX_RECENT_FILES )
91  {
92  removeRecentFile( mRecentFiles.last() );
93  }
94 
95  mRecentFiles.prepend( filename );
96 
97  QAction* action = new QAction( filename, this );
98  action->setData( QVariant( filename ) );
99 
100  QObject::connect( action, SIGNAL( triggered() ), this, SLOT( onRecentFileTriggered() ) );
101 
102  mRecentActions.emplace( filename, action );
103  if ( mRecentFiles.size() == 1 )
104  {
105  addAction( action );
106  addAction( mClearSeparator );
107  addAction( mClearAction );
108  QObject::connect( mClearAction, &QAction::triggered, this, &RecentFileMenu::clear );
109  }
110  else
111  {
112  QString firstFile = mRecentFiles[ 1 ];
113  qDebug() << "Recent file" << firstFile;
114  insertAction( mRecentActions[ firstFile ], action );
115  }
116 }
117 
118 void RecentFileMenu::removeRecentFile( QString filename )
119 {
120  if ( mRecentFiles.contains( filename ) )
121  {
122  QAction *action = mRecentActions.at( filename );
123  removeAction( action );
124 
125  mRecentActions.erase( filename );
126  mRecentFiles.removeOne( filename );
127  delete action;
128  }
129 }
130 
131 void RecentFileMenu::onRecentFileTriggered()
132 {
133  QAction* action = ( QAction* )QObject::sender();
134  QString filePath = action->data().toString();
135 
136  if ( !filePath.isEmpty() )
137  {
138  emit loadRecentFile( filePath );
139  }
140 }