File: ccRecentFiles.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (163 lines) | stat: -rw-r--r-- 4,105 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: CloudCompare project                               #
//#                                                                        #
//##########################################################################

#include <QAction>
#include <QDir>
#include <QFile>
#include <QMenu>
#include <QSettings>
#include <QString>
#include <QStringList>

#include "ccRecentFiles.h"
#include "mainwindow.h"


QString ccRecentFiles::s_settingKey( "RecentFiles" );


ccRecentFiles::ccRecentFiles( QWidget *parent ) :
	QObject( parent )
{
	m_menu = new QMenu( tr("Open Recent..."), parent );
	
	m_actionClearMenu = new QAction( tr("Clear Menu"), this );
	
	connect( m_actionClearMenu, &QAction::triggered, this, [this]() {
		m_settings.remove( s_settingKey );
		
		updateMenu();		
	});
	
	updateMenu();
}

QMenu *ccRecentFiles::menu()
{
	return m_menu;
}

void ccRecentFiles::addFilePath( const QString &filePath )
{
	QStringList	list = m_settings.value( s_settingKey ).toStringList();
	
	list.removeAll( filePath );
	list.prepend( filePath );
	
	// only save the last ten files
	if ( list.count() > 10 )
	{
		list = list.mid( 0, 10 );
	}
	
	m_settings.setValue( s_settingKey, list );
	
	updateMenu();
}

void ccRecentFiles::updateMenu()
{
	m_menu->clear();
	
	const QStringList	recentList = listRecent();
	
	for ( const QString &recentFile : recentList )
	{
		QAction  *recentAction = new QAction( contractFilePath( recentFile ), this );
		
		recentAction->setData( recentFile );
		
		connect( recentAction, &QAction::triggered, this, &ccRecentFiles::openFileFromAction );
		
		m_menu->addAction( recentAction );
	}
	
	if ( !m_menu->actions().isEmpty() )
	{
		m_menu->addSeparator();
		m_menu->addAction( m_actionClearMenu );
	}
	
	m_menu->setEnabled( !m_menu->actions().isEmpty() );
}

void ccRecentFiles::openFileFromAction()
{
	QAction  *action = qobject_cast<QAction *>( sender() );
	
	Q_ASSERT( action );
	
	QString  fileName = action->data().toString();
	
	if ( !QFile::exists( fileName ) )
	{
		return;
	}
	
	QStringList	fileListOfOne{ fileName };
	
	MainWindow::TheInstance()->addToDB( fileListOfOne );
}

QStringList ccRecentFiles::listRecent()
{	
	QStringList list = m_settings.value( s_settingKey ).toStringList();
	
	QStringList::iterator iter = list.begin();
	
	while ( iter != list.end() )
	{
		const QString filePath = *iter;
		
		if ( !QFile::exists( filePath ) )
		{
			iter = list.erase( iter );
			continue;
		}
		
		++iter;
	}
	
	return list;
}

QString ccRecentFiles::contractFilePath( const QString &filePath )
{
	QString  homePath = QDir::toNativeSeparators( QDir::homePath() );
	QString  newPath = QDir::toNativeSeparators( filePath );
	
	if ( newPath.startsWith( homePath ) )
	{
		return newPath.replace( 0, QDir::homePath().length(), '~' );
	}
	
	return filePath;
}

QString ccRecentFiles::expandFilePath( const QString &filePath )
{
	QString  newPath = QDir::toNativeSeparators( filePath );
	
	if ( newPath.startsWith( '~' ) )
	{
		QString  homePath = QDir::toNativeSeparators( QDir::homePath() );
		
		return newPath.replace( 0, 1, homePath );
	}
	
	return filePath;
}