]> Creatis software - clitk.git/blob - vv/vvUtils.cxx
Initial revision
[clitk.git] / vv / vvUtils.cxx
1 /*=========================================================================
2
3  Program:   vv
4  Language:  C++
5  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24
25 #include <fstream>
26 #include <algorithm>
27 #include <QDir>
28
29 #include "clitkCommon.h"
30 #include "vvUtils.h"
31
32 const std::string vv_user_file=".vv_settings.txt";
33 typedef std::list<std::string> FileListType;
34
35 ///Returns the last images opened by the user
36 FileListType GetRecentlyOpenedImages()
37 {
38     std::ifstream in((QDir::homePath().toStdString() + "/" + vv_user_file).c_str());
39     std::string current_file;
40     FileListType result;
41     in >> current_file;
42     while (in.good())
43     {
44         result.push_back(current_file);
45         in >> current_file;
46     }
47     in.close();
48     return result;
49 }
50
51 ///Adds an image to the list of recently opened images
52 void AddToRecentlyOpenedImages(std::string filename)
53 {
54     FileListType file_list = GetRecentlyOpenedImages();
55     FileListType::iterator i = std::find(file_list.begin(),file_list.end(),filename);
56     if (i != file_list.end()) // avoid dupes
57         file_list.erase(i);
58     while (file_list.size() >= 6) //keep list to a reasonable size
59         file_list.pop_back();
60     file_list.push_front(filename);
61     std::ofstream out((QDir::homePath().toStdString() + "/" + vv_user_file).c_str(),std::ios_base::out | std::ios_base::trunc);
62     for (FileListType::iterator j = file_list.begin() ; j != file_list.end() ; j++)
63         out << (*j) << std::endl;
64     out.close();
65 }