VPTissue Reference Manual
BackgroundDialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "BackgroundDialog.h"
21 
23 
24 #include <cmath>
25 #include <QBoxLayout>
26 #include <QCheckBox>
27 #include <QDir>
28 #include <QFileDialog>
29 #include <QGraphicsPixmapItem>
30 #include <QLineEdit>
31 #include <QMessageBox>
32 #include <QPushButton>
33 #include <QString>
34 
35 class QWidget;
36 
37 namespace SimPT_Editor {
38 
39 BackgroundDialog::BackgroundDialog(QGraphicsPixmapItem *item, QWidget *parent)
40  : QDialog(parent), m_item(item)
41 {
42  SetupGui();
43 }
44 
46 {
47 }
48 
49 void BackgroundDialog::BrowseImage()
50 {
51  QString lastFile = m_file_path->text().isEmpty() ? QDir::homePath() : QFileInfo(m_file_path->text()).absolutePath();
52  //Native dialog has been disabled, due to a bug when an item is selected.
53  QString filePath = QFileDialog::getOpenFileName(this, "Select the background image file to load", lastFile, "Images (*.png *.xpm *.jpg *.bmp);;All files (*.*)", nullptr, QFileDialog::DontUseNativeDialog | QFileDialog::ReadOnly);
54 
55  if (!filePath.isEmpty() && filePath != m_file_path->text()) {
56  if (m_pixmap.load(filePath)) {
57  m_item->setPixmap(m_pixmap);
58  UpdateTransformation();
59  m_file_path->setText(filePath);
60  m_visible->setEnabled(true);
61  m_visible->setChecked(true);
62  m_transformation->setEnabled(true);
63  } else {
64  QMessageBox::critical(this, "Error", "Couldn't load the image", QMessageBox::Ok);
65  m_visible->setEnabled(false);
66  m_transformation->setEnabled(false);
67  }
68  }
69 }
70 
71 void BackgroundDialog::UpdateVisibility(bool visible)
72 {
73  m_item->setVisible(visible);
74  m_transformation->setEnabled(visible);
75 }
76 
77 void BackgroundDialog::UpdateTransformation()
78 {
79  QTransform transformation;
80 
81  QPointF center = m_item->boundingRect().center();
82 
83  transformation.translate(m_transformation->GetTranslationX(), m_transformation->GetTranslationY());
84  transformation.rotate(m_transformation->GetRotation());
85  transformation.scale(m_transformation->GetScalingX(), m_transformation->GetScalingY());
86 
87  transformation.translate(-center.x(), -center.y());
88 
89  m_item->setTransform(transformation);
90 }
91 
92 void BackgroundDialog::SetupGui()
93 {
94  setWindowTitle("Set background image");
95  setMinimumWidth(350);
96 
97  // > GUI layout
98  QVBoxLayout *layout = new QVBoxLayout();
99 
100  // > File selection
101  QHBoxLayout *fileLayout = new QHBoxLayout();
102 
103  m_file_path = new QLineEdit();
104  m_file_path->setReadOnly(true);
105  fileLayout->addWidget(m_file_path);
106 
107  QPushButton *browseButton = new QPushButton("Browse...");
108  connect(browseButton, SIGNAL(clicked()), this, SLOT(BrowseImage()));
109  fileLayout->addWidget(browseButton);
110 
111  layout->addLayout(fileLayout);
112  // < File selection
113 
114  // > Image visibility
115  m_visible = new QCheckBox("Display image");
116  m_visible->setEnabled(false);
117  m_visible->setChecked(false);
118  connect(m_visible, SIGNAL(toggled(bool)), this, SLOT(UpdateVisibility(bool)));
119  layout->addWidget(m_visible);
120  // < Image visibility
121 
122  // > Transformations
123  m_transformation = new TransformationWidget(10, 3 * std::max(m_item->boundingRect().width(), 200.0), 3 * std::max(m_item->boundingRect().height(), 200.0));
124  m_transformation->setEnabled(false);
125  connect(m_transformation, SIGNAL(TransformationChanged()), this, SLOT(UpdateTransformation()));
126  layout->addWidget(m_transformation);
127  // < Transformations
128 
129  setLayout(layout); // < GUI layout
130 }
131 
132 } // namespace
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
BackgroundDialog(QGraphicsPixmapItem *item, QWidget *parent=nullptr)
Constructor.
virtual ~BackgroundDialog()
Destructor.
double GetTranslationY()
Gets the y translation of the transformation.
Interface for TransformationWidget.
int GetRotation()
Gets the rotation of the transformation.
double GetScalingX()
Gets the x scaling of the transformation.
double GetTranslationX()
Gets the x translation of the transformation.
double GetScalingY()
Gets the y scaling of the transformation.
Interface for BackgroundDialog.
see the online Qt documentation
see the online Qt documentation