~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/VISUAL/APPLICATIONS/INIFileEditorWindow.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Timo Sachsenberg $
 
25
// $Authors: Marc Sturm $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#include <OpenMS/VISUAL/APPLICATIONS/INIFileEditorWindow.h>
 
29
#include <OpenMS/CONCEPT/LogStream.h>
 
30
#include <OpenMS/SYSTEM/File.h>
 
31
 
 
32
#include <QtGui/QToolBar>
 
33
#include <QtCore/QString>
 
34
#include <QtGui/QFileDialog>
 
35
#include <QtGui/QMenu>
 
36
#include <QtGui/QMenuBar>
 
37
#include <QtGui/QMessageBox>
 
38
#include <QtGui/QCloseEvent>
 
39
#include <QtGui/QGridLayout>
 
40
#include <QtGui/QCheckBox>
 
41
 
 
42
using namespace std;
 
43
 
 
44
namespace OpenMS
 
45
{
 
46
 
 
47
        INIFileEditorWindow::INIFileEditorWindow(QWidget *parent) 
 
48
                : QMainWindow(parent),
 
49
                        current_path_(".")
 
50
        {
 
51
                setWindowTitle("INIFileEditor");
 
52
          setWindowIcon(QIcon(":/INIFileEditor.png"));
 
53
 
 
54
                //create central widget and layout
 
55
                QWidget* central_widget = new QWidget;
 
56
                setCentralWidget(central_widget);
 
57
                QGridLayout* layout = new QGridLayout(central_widget);
 
58
                
 
59
                //create advanced check box and ParamEditor and connect them
 
60
                editor_=new ParamEditor(central_widget);
 
61
                layout->addWidget(editor_,0,0,1,2);
 
62
                
 
63
                QMenu* file = new QMenu("&File",this);
 
64
                menuBar()->addMenu(file);
 
65
                file->addAction("&Open",this,SLOT(openFile()), Qt::CTRL+Qt::Key_O);
 
66
                file->addSeparator();
 
67
                file->addAction("&Save",this,SLOT(saveFile()), Qt::CTRL+Qt::Key_S);
 
68
                file->addAction("Save &As",this,SLOT(saveFileAs()));
 
69
                file->addSeparator();
 
70
                file->addAction("&Quit",this,SLOT(close()));
 
71
                
 
72
                // we connect the "changes state"(changes made/no changes) signal from the ParamEditor to the window title updating slot
 
73
                connect(editor_,SIGNAL(modified(bool)),this,SLOT(updateWindowTitle(bool)));
 
74
                
 
75
                setMinimumSize(600,600);
 
76
        }
 
77
        
 
78
        bool INIFileEditorWindow::openFile(const String& filename)
 
79
        {
 
80
                if (filename=="")
 
81
                {
 
82
                        filename_=QFileDialog::getOpenFileName(this,tr("Open ini file"),current_path_.toQString(),tr("ini files (*.ini);; all files (*.*)"));
 
83
                }
 
84
                else
 
85
                {
 
86
                        filename_ = filename.c_str();
 
87
                }
 
88
                
 
89
                if(!filename_.isEmpty())
 
90
                {
 
91
                        if (File::readable(filename_.toStdString()))
 
92
                        {
 
93
                                param_.clear();
 
94
                                try
 
95
                                {
 
96
                                        param_.load(filename_.toStdString());
 
97
                                        editor_->load(param_);
 
98
                                        updateWindowTitle(editor_->isModified());
 
99
                                        return true;
 
100
                                }
 
101
                                catch (Exception::BaseException &e)
 
102
                                {
 
103
                                        LOG_ERROR << "Error while parsing file '" << filename_.toStdString() << "'\n";
 
104
                                        LOG_ERROR << e << "\n";
 
105
                                }
 
106
                        }
 
107
 
 
108
                        QMessageBox::critical(this,"Error opening file",("The file '"+filename_.toStdString()+"' does not exist, is not readable or not a proper INI file!").c_str());          
 
109
                }
 
110
                return false;
 
111
        }
 
112
        
 
113
        
 
114
        bool INIFileEditorWindow::saveFile()
 
115
        {
 
116
                if(filename_.isEmpty())
 
117
                {
 
118
                        return false;
 
119
                }
 
120
                
 
121
                editor_->store();
 
122
 
 
123
                param_.store(filename_.toStdString());
 
124
                updateWindowTitle(editor_->isModified());
 
125
                return true;
 
126
        }
 
127
        
 
128
        
 
129
        bool INIFileEditorWindow::saveFileAs()
 
130
        {
 
131
                filename_=QFileDialog::getSaveFileName(this,tr("Save ini file"),current_path_.toQString(),tr("ini files (*.ini)"));
 
132
                if(!filename_.isEmpty())
 
133
                {
 
134
                        if(!filename_.endsWith(".ini")) filename_.append(".ini");
 
135
                        
 
136
                        editor_->store();
 
137
                        
 
138
                        param_.store(filename_.toStdString());
 
139
                        updateWindowTitle(editor_->isModified());
 
140
                        return true;
 
141
                }
 
142
                return false;
 
143
        }
 
144
        
 
145
        void INIFileEditorWindow::closeEvent(QCloseEvent* event)
 
146
        {
 
147
                if(editor_->isModified())
 
148
                {
 
149
                        QMessageBox::StandardButton result=QMessageBox::question(this,"Save?","Do you want to save your changes?",QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::Discard);
 
150
                        if (result==QMessageBox::Ok)
 
151
                        {
 
152
                                if(saveFile())
 
153
                                {
 
154
                                        event->accept();
 
155
                                }
 
156
                                else
 
157
                                {
 
158
                                        event->ignore();
 
159
                                }
 
160
                        }
 
161
                        else if(result==QMessageBox::Cancel)
 
162
                        {
 
163
                                event->ignore();
 
164
                        }
 
165
                        else
 
166
                        {
 
167
                                event->accept();
 
168
                        }
 
169
                }
 
170
                else
 
171
                {
 
172
                        event->accept();
 
173
                }
 
174
        }
 
175
        
 
176
        void INIFileEditorWindow::updateWindowTitle(bool update)
 
177
        {
 
178
                //update window title                   
 
179
                if(update)
 
180
                {
 
181
                        setWindowTitle((File::basename(filename_) + " * - INIFileEditor").toQString());
 
182
                }
 
183
                else
 
184
                {
 
185
                        setWindowTitle((File::basename(filename_) + " - INIFileEditor").toQString());
 
186
                }
 
187
                
 
188
                //update last path as well
 
189
                current_path_ = File::path(filename_);
 
190
        }
 
191
}
 
192