]> Creatis software - clitk.git/blob - vv/vvUtils.cxx
factorising parameters for QSettings
[clitk.git] / vv / vvUtils.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #include <sstream>
19 #include <algorithm>
20 #include <QDir>
21 #include "clitkCommon.h"
22 #include "vvUtils.h"
23
24 const std::string vv_user_file=".vv_settings.txt";
25 const std::string recentFileList="recentFiles";
26 typedef std::list<std::string> FileListType;
27
28 QString getVVSettingsPath(){
29   return QDir::homePath()+QString::fromStdString("/"+vv_user_file);
30 }
31
32 QSettings::Format getSettingsOptionFormat(){
33   return QSettings::NativeFormat;
34 }
35
36 ///Returns the last images opened by the user
37 FileListType GetRecentlyOpenedImages()
38 {
39   QSettings settings(getVVSettingsPath(), getSettingsOptionFormat());
40   FileListType result;
41   settings.beginGroup(QString::fromStdString(recentFileList));
42     QStringList keys = settings.childKeys();
43     for(int i=0; i<keys.size(); i++){
44       std::string value=settings.value(QString::fromStdString (keys[i].toStdString())).toString().toStdString();
45       result.push_back(value);
46     }
47   settings.endGroup();
48   return result;
49 }
50
51 ///Adds an image to the list of recently opened images
52 void AddToRecentlyOpenedImages(std::string filename)
53 {
54   QSettings settings(getVVSettingsPath(), getSettingsOptionFormat());
55   FileListType file_list = GetRecentlyOpenedImages();
56   
57   FileListType::iterator i = std::find(file_list.begin(),file_list.end(),filename);
58   if (i != file_list.end()) // avoid dupes
59     file_list.erase(i);
60   while (file_list.size() >= 6) //keep list to a reasonable size
61     file_list.pop_back();
62   file_list.push_front(filename);
63   
64   settings.beginGroup(QString::fromStdString(recentFileList));
65     int index=0;
66     for (FileListType::iterator j = file_list.begin() ; j != file_list.end() ; j++){
67       QString s=QString(index++);
68       settings.setValue(s, QString::fromStdString ( *j ));
69     }
70   settings.endGroup();
71 }
72