1 /***************************************************************************
2  *   Copyright (C) 2000-2019 by Johan Maes                                 *
3  *   on4qz@telenet.be                                                      *
4  *   http://users.telenet.be/on4qz                                         *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21 #include "editor.h"
22 
23 #include <QtGui>
24 #include "appglobal.h"
25 #include "dirdialog.h"
26 #include "editorview.h"
27 #include "configparams.h"
28 
29 #ifndef STANDALONE
30 #include "dispatcher.h"
31 #endif
32 
33 /*!
34   constructor
35 */
editor(QWidget * parent)36 editor::editor(QWidget *parent): QMainWindow(parent)
37 {
38   ev=new editorView(this);
39   setCentralWidget (ev);
40   initActions();
41   initMenubar();
42   statusBar()->showMessage("Select a tool");
43   setMinimumSize(640,480);
44   resize(640,480);
45   addToLog (QString(" editor create: %1")
46             .arg(QString::number((ulong)this,16)),LOGEDIT);
47   setWindowTitle("Template Editor");
48   readSettings();
49 }
50 
51 /*!
52   destructor (saves settings on deletion)
53 */
54 
~editor()55 editor::~editor()
56 {
57   writeSettings();
58 }
59 
60 /*!
61   reads the settings (saved images for tx,rx,templates)
62 */
63 
readSettings()64 void editor::readSettings()
65 {
66   QSettings qSettings;
67   qSettings.beginGroup ("Editor" );
68   int windowWidth = qSettings.value( "windowWidth", 640 ).toInt();
69   int windowHeight = qSettings.value( "windowHeight", 480 ).toInt();
70   int windowX = qSettings.value( "windowX", -1 ).toInt();
71   int windowY = qSettings.value( "windowY", -1 ).toInt();
72   if ( windowX != -1 || windowY != -1 ) 	move ( windowX, windowY );
73   resize ( windowWidth, windowHeight );
74   qSettings.endGroup();
75 }
76 
77 /*!
78   writes the settings (saved images for tx,rx,templates)
79 */
writeSettings()80 void editor::writeSettings()
81 {
82   QSettings qSettings;
83   qSettings.beginGroup ("Editor" );
84   qSettings.setValue ( "windowWidth", width() );
85   qSettings.setValue ( "windowHeight", height() );
86   qSettings.setValue ( "windowX", x() );
87   qSettings.setValue ( "windowY", y() );
88   qSettings.endGroup();
89 }
90 
91 
initActions()92 void editor::initActions()
93 {
94   fileNew = new QAction(QIcon(":/icons/filenew.png"),tr("&New"),this);
95   fileNew->setShortcut(tr("Ctrl+N"));
96   fileNew->setStatusTip(tr("Create a new image"));
97   connect(fileNew, SIGNAL(triggered()), this, SLOT(slotFileNew()));
98 
99   fileOpen = new QAction(QIcon(":/icons/fileopen.png"),tr("&Open"),this);
100   fileOpen->setShortcut(tr("Ctrl+O"));
101   fileOpen->setStatusTip(tr("Open an image file"));
102   connect(fileOpen, SIGNAL(triggered()), this, SLOT(slotFileOpen()));
103 
104   fileSave = new QAction(QIcon(":/icons/filesave.png"),tr("&Save file"),this);
105   fileSave->setStatusTip(tr("Save the file under the same name and format"));
106   connect(fileSave, SIGNAL(triggered()), this, SLOT(slotFileSave()));
107 
108   fileSaveImage = new QAction(tr("Save as &Image"),this);
109   fileSaveImage->setStatusTip(tr("Save the file in PNG format"));
110   connect(fileSaveImage, SIGNAL(triggered()), this, SLOT(slotFileSaveImage()));
111 
112   fileSaveTemplate = new QAction(("Save as &Template"),this);
113   fileSaveTemplate->setStatusTip(tr("Save template file "));
114   connect(fileSaveTemplate, SIGNAL(triggered()), this, SLOT(slotFileSaveTemplate()));
115 
116   fileQuit = new QAction(tr("Quit"),this);
117   fileQuit->setShortcut(tr("Ctrl+Q"));
118   fileQuit->setStatusTip(tr("Quits the editor"));
119   connect(fileQuit, SIGNAL(triggered()), this, SLOT(slotFileQuit()));
120 
121   clearAll= new QAction(QIcon(":/icons/eraser.png"),tr("Clear &All"),this);
122   clearAll->setShortcut(tr("Ctrl+A"));
123   clearAll->setStatusTip(tr("Delete all objects and fill the background with the background color"));
124   connect(clearAll, SIGNAL(triggered()), ev, SLOT(slotClearAll()));
125 
126   copy= new QAction(tr("Copy"),this);
127   copy->setShortcut(tr("Ctrl+C"));
128   connect(copy, SIGNAL(triggered()), ev->getScene(), SLOT(slotCopy()));
129 
130   paste= new QAction(tr("Paste"),this);
131   paste->setShortcut(tr("Ctrl+V"));
132   connect(paste, SIGNAL(triggered()), ev->getScene(), SLOT(slotPaste()));
133 
134   deleteAction=new QAction(tr("&Delete"),this);
135   deleteAction->setShortcut(tr("Del"));
136   connect(deleteAction, SIGNAL(triggered()), ev->getScene(), SLOT(slotDeleteItem()));
137 }
138 
139 
140 
initMenubar()141 void editor::initMenubar()
142 {
143   fileMenu=menuBar()->addMenu(tr("&File"));
144   editMenu=menuBar()->addMenu(tr("&Edit"));
145   fileMenu->addAction(fileNew);
146   fileMenu->addAction(fileOpen);
147   fileMenu->addAction(fileSave);
148   fileMenu->addAction(fileSaveImage);
149   fileMenu->addAction(fileSaveTemplate);
150   fileMenu->addAction(fileQuit);
151   editMenu->addAction(deleteAction);
152   editMenu->addAction(copy);
153   editMenu->addAction(paste);
154   editMenu->addAction(clearAll);
155 }
156 
157 
slotFileNew()158 void editor::slotFileNew()
159 {
160   if(ev->isModified())
161     {
162       switch( QMessageBox::information( this, "Editor",
163                                         "The document has not been saved as a template\n",
164                                         "&Continue Anyway","Cancel",NULL,
165                                         -1,      // Enter == button 0
166                                         1 ) )
167         { // Escape == button 2
168         case 0: // Continu clicked
169           break;
170         case 1: // Cancel clicked
171           return;
172           break;
173         }
174     }
175   ev->slotClearAll();
176   localFile.close();
177   localFile.setFileName("Untitled.templ");
178   setWindowTitle(QString("Template Editor: %1").arg(localFile.fileName()));
179 }
180 
slotFileOpen()181 void editor::slotFileOpen()
182 {
183   /*	QFileDialog *fd = new QFileDialog(this,0,true);
184   fd->show();*/
185   dirDialog d(this,0);
186   QString s=d.openFileName(templatesPath,"*.png *.gif *.jpg *.templ");
187   if (s.isNull()) return ;
188   if (s.isEmpty()) return ;
189   localFile.setFileName(s);
190   if(ev->open(localFile))
191     {
192       setWindowTitle(QString("Template Editor: %1").arg(s));
193       addToLog("localfile after open = " + localFile.fileName(),LOGEDIT);
194     }
195 }
196 
197 /*!
198     \fn editor::slotFileSave()
199     \brief save file under same name and same type
200 */
201 
slotFileSave()202 void editor::slotFileSave()
203 {
204   if(localFile.fileName().isEmpty())
205     {
206       slotFileSaveTemplate();
207       return;
208     }
209   if(ev->getScene()->getImageType()==editorScene::FLATIMAGE)
210     {
211       addToLog("localfile to save = " + localFile.fileName(),LOGEDIT);
212       ev->save(localFile,false);
213     }
214   else
215     {
216       ev->save(localFile,true);
217     }
218 }
219 
slotFileSaveImage()220 void editor::slotFileSaveImage()
221 {
222   dirDialog d((QWidget *)this,"Editor");
223   QString s(localFile.fileName());
224   if(s.isEmpty())
225     {
226       s=txStockImagesPath;
227     }
228   s=d.saveFileName(s,"*.png","png");
229   if (s.isNull()) return ;
230   if (s.isEmpty()) return ;
231   localFile.setFileName(s);
232   setWindowTitle(QString("Template Editor: %1").arg(s));
233   ev->save(localFile,false);
234 }
235 
slotFileSaveTemplate()236 void editor::slotFileSaveTemplate()
237 {
238   dirDialog d((QWidget *)this,"Browse");
239   QString s(localFile.fileName());
240   if(s.isEmpty())
241     {
242       s=templatesPath;
243     }
244   s=d.saveFileName(s,"*.templ","templ");
245   if (s.isNull()) return ;
246   if (s.isEmpty()) return ;
247   localFile.setFileName(s);
248   setWindowTitle(QString("Template Editor: %1").arg(s));
249   ev->save(localFile,true);
250 }
251 
252 
slotFileQuit()253 void editor::slotFileQuit()
254 {
255   ev->writeSettings();
256   close();
257 }
258 
259 
260 
closeEvent(QCloseEvent * e)261 void editor::closeEvent(QCloseEvent *e)
262 {
263 
264   if(ev->isModified())
265     {
266       QMessageBox msgBox;
267       msgBox.setText("The document has been modified.");
268       msgBox.setInformativeText("Do you want to save your changes?");
269       msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
270       msgBox.setDefaultButton(QMessageBox::Save);
271       int ret = msgBox.exec();
272       switch (ret)
273         {
274         case QMessageBox::Save:
275           slotFileSave();
276           break;
277         case QMessageBox::Discard:
278           // Don't Save was clicked
279           break;
280         case QMessageBox::Cancel:
281           return;
282           break;
283         default:
284           // should never be reached
285           break;
286         }
287     }
288 #ifndef STANDALONE
289   editorFinishedEvent *ce = new editorFinishedEvent(true,localFile.fileName());
290   QApplication::postEvent(dispatcherPtr, ce );  // Qt will delete it when done	emit imageAvailable(ev->getImage());
291 #endif
292   writeSettings();
293   e->accept();
294 }
295 
296 
297 
setImage(QImage * im)298 bool editor::setImage(QImage *im)
299 {
300   ev->setImage(im);
301   return true;
302 }
303 
openFile(QString fn)304 bool editor::openFile(QString fn)
305 {
306   QFile f(fn);
307   localFile.setFileName(fn);
308   return ev->open(f);
309 }
310 
311