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

« back to all changes in this revision

Viewing changes to source/VISUAL/DIALOGS/SaveImageDialog.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
 
 
29
#include <iostream>
 
30
#include <cmath>
 
31
 
 
32
#include <OpenMS/VISUAL/DIALOGS/SaveImageDialog.h>
 
33
#include <OpenMS/MATH/MISC/MathFunctions.h>
 
34
 
 
35
// Qt
 
36
#include <QtGui/QLayout>
 
37
#include <QtGui/QPushButton>  
 
38
#include <QtGui/QComboBox>
 
39
#include <QtGui/QLabel>
 
40
#include <QtGui/QValidator>
 
41
#include <QtGui/QGridLayout>
 
42
#include <QtGui/QHBoxLayout>
 
43
#include <QtGui/QImageWriter>
 
44
#include <QtGui/QApplication>
 
45
 
 
46
namespace OpenMS
 
47
{
 
48
 
 
49
 
 
50
        SaveImageDialog::SaveImageDialog( QWidget * parent ):
 
51
        QDialog(parent)
 
52
        {
 
53
                size_ratio_=1;
 
54
                //create dialog and layout (grid)
 
55
                QGridLayout* grid=new QGridLayout(this);
 
56
                
 
57
                //add accept/cancel buttons (and their layout)
 
58
                QBoxLayout* box_layout = new QHBoxLayout();
 
59
                grid->addLayout(box_layout,5,1);
 
60
                
 
61
                QPushButton* button = new QPushButton(this);
 
62
                button->setText("Cancel");
 
63
                connect(button,SIGNAL(clicked()),this,SLOT(reject()));
 
64
                box_layout->addWidget(button);
 
65
                
 
66
                button = new QPushButton(this);
 
67
                button->setText("Accept");
 
68
                button->setDefault(true);
 
69
                connect(button,SIGNAL(clicked()),this,SLOT(checkSize()));
 
70
                box_layout->addWidget(button);
 
71
        
 
72
                //add picture format selector
 
73
                QLabel* label = new QLabel("Picture format:",this);
 
74
                grid->addWidget(label,0,0);
 
75
                format_ = new QComboBox(this);
 
76
                QList<QByteArray> list = QImageWriter::supportedImageFormats();
 
77
                for (int i = 0; i < list.size(); ++i)
 
78
                {
 
79
                        format_->insertItem(i,list.at(i));
 
80
                }
 
81
                grid->addWidget(format_,0,1,Qt::AlignLeft);
 
82
                //set format to PNG/JPEG if available
 
83
                int png=-1;
 
84
                int jpeg=-1;
 
85
                for (int i=0;i<format_->count();i++)
 
86
                {       
 
87
                        if (format_->itemText(i)=="PNG")
 
88
                        {
 
89
                                png=i;
 
90
                        }
 
91
                        if (format_->itemText(i)=="JPEG")
 
92
                        {
 
93
                                jpeg=i;
 
94
                        }                       
 
95
                }
 
96
                if (png!=-1)
 
97
                {
 
98
                        format_->setCurrentIndex(png);
 
99
                }
 
100
                else if (jpeg!=-1)
 
101
                {
 
102
                        format_->setCurrentIndex(jpeg);
 
103
                }
 
104
                
 
105
                //add size boxes and label (and their layout)
 
106
                label = new QLabel("Size (WxH):",this);
 
107
                grid->addWidget(label,1,0);
 
108
                
 
109
                QValidator* v = new QIntValidator(1,10000,this);
 
110
                box_layout = new QHBoxLayout();
 
111
                grid->addLayout(box_layout,1,1);
 
112
                size_x_ = new QLineEdit(this);
 
113
                size_x_->setValidator(v);
 
114
                connect(size_x_,SIGNAL(textChanged(const QString&)),this,SLOT(xSizeChanged(const QString&)));
 
115
                box_layout->addWidget(size_x_);
 
116
                label = new QLabel("x",this);
 
117
                box_layout->addWidget(label);
 
118
                size_y_ = new QLineEdit(this);
 
119
                size_y_->setValidator(v);
 
120
                connect(size_y_,SIGNAL(textChanged(const QString&)),this,SLOT(ySizeChanged(const QString&)));
 
121
                box_layout->addWidget(size_y_);
 
122
                label = new QLabel("pixel",this);
 
123
                box_layout->addWidget(label);           
 
124
                
 
125
                size_proportions_ = new QCheckBox("keep proportions",this);
 
126
                size_proportions_->setChecked(true);
 
127
                connect(size_proportions_,SIGNAL(toggled(bool)),this,SLOT(proportionsActivated(bool)));
 
128
                grid->addWidget(size_proportions_,2,1);
 
129
        }
 
130
        
 
131
        void SaveImageDialog::setSize(int x, int y)
 
132
        {
 
133
                QString* temp =new QString();
 
134
                temp->setNum(x);
 
135
                size_x_->setText(*temp);
 
136
                temp->setNum(y);
 
137
                size_y_->setText(*temp);
 
138
                setSizeRatio_(float(x)/float(y));
 
139
        }
 
140
                
 
141
        void SaveImageDialog::setSizeRatio_(float r)
 
142
        {
 
143
                if (r==0.0)
 
144
                {
 
145
                        size_ratio_=1.0;
 
146
                }
 
147
                else
 
148
                {
 
149
                        size_ratio_=r;
 
150
                }
 
151
        }       
 
152
                        
 
153
        void SaveImageDialog::xSizeChanged(const QString& s)
 
154
        {
 
155
                if(size_proportions_->isChecked() && size_x_==qApp->focusWidget())
 
156
                {
 
157
                        QString* temp = new QString();
 
158
                        temp->setNum((int)Math::round(s.toInt()/size_ratio_));
 
159
                        size_y_->setText(*temp);
 
160
                }
 
161
        }       
 
162
        
 
163
        void SaveImageDialog::ySizeChanged(const QString& s)
 
164
        {
 
165
                if(size_proportions_->isChecked() && size_y_==qApp->focusWidget())
 
166
                {
 
167
                        QString* temp = new QString();
 
168
                        temp->setNum((int)Math::round(s.toInt()*size_ratio_));
 
169
                        size_x_->setText(*temp);
 
170
                }
 
171
        }       
 
172
        
 
173
        void SaveImageDialog::proportionsActivated(bool state)
 
174
        {
 
175
                if (state==true){
 
176
                        setSizeRatio_(QString(size_x_->text()).toFloat()/QString(size_y_->text()).toFloat());
 
177
                        }
 
178
        }       
 
179
        
 
180
        void SaveImageDialog::checkSize()
 
181
        {
 
182
                int x =size_x_->text().toInt();
 
183
                int y =size_y_->text().toInt();
 
184
                if (x>0 && y>0){
 
185
                        accept();
 
186
                        }
 
187
        }
 
188
        
 
189
        int SaveImageDialog::getXSize()
 
190
        {
 
191
                return size_x_->text().toInt();
 
192
        }
 
193
        
 
194
        int SaveImageDialog::getYSize()
 
195
        {
 
196
                return size_y_->text().toInt();
 
197
        }
 
198
        
 
199
        QString SaveImageDialog::getFormat()
 
200
        {
 
201
                return format_->currentText();
 
202
        }
 
203
 
 
204
} //namespace
 
205