/*************************************************************************** * Copyright (C) 2004 by Johan Maes - ON4QZ * * on4qz@telenet.be * * http://users.telenet.be/on4qz * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "editor.h" #include "qsstvglobal.h" #include "editorform.h" #include "editorview.h" #include #include #include #include "colorselector.h" #include "fgbgbutton.h" #include "utils.h" #include #include #include #include "configdialog.h" static QColor defaultColors[NUMCOLORSELECTORS]= { qRgba( 0, 0, 0,255), qRgba(255,255,255,255), qRgba(255, 0, 0,255), qRgba( 0,255, 0,255), qRgba( 0, 0,255,255), qRgba(128,128,128,255), qRgba(255, 0, 0,128), qRgba( 0,255, 0,128), qRgba( 0, 0,255,128), qRgba(255,255, 0,128), qRgba( 0,255,255,128), qRgba(255, 0,255,128) }; //editor::editor(QWidget *parent, const char *name): QMainWindow(parent, name,Qt::WType_Dialog | Qt::WShowModal) editor::editor(QWidget *parent, const char *name): QMainWindow(parent, name,Qt::WType_Dialog ) { initView(); readSettings(); initActions(); initToolbar(); initMenubar(); ev->editorCanvas->setStatusBar(statusBar()); statusBar()->message("Select a tool"); } editor::~editor() { } void editor::readSettings() { int i; QSettings qSettings(QSettings::Ini); qSettings.beginGroup(APP_KEY+"/EDITOR/"); int windowWidth = qSettings.readNumEntry("rxWindowWidth", 460 ); int windowHeight = qSettings.readNumEntry("rxWindowHeight", 530 ); int windowX = qSettings.readNumEntry( "rxWindowX", -1 ); int windowY = qSettings.readNumEntry( "rxWindowY", -1 ); resize( windowWidth, windowHeight ); if ( windowX != -1 || windowY != -1 ) move( windowX, windowY ); for (i=0;isetColor(qSettings.readNumEntry( "csColor"+QString::number(i),defaultColors[i].rgb())); } ev->fgbgFrame->setForegroundColor(qSettings.readNumEntry("fgColor",qRgb(0,0,0))); ev->fgbgFrame->setBackgroundColor(qSettings.readNumEntry("bgColor",qRgb(255,255,255))); ev->editorCanvas->setBrushStyle(qSettings.readNumEntry("brushStyle",Qt::SolidPattern)); ev->editorCanvas->setFont(qSettings.readEntry("font","Sans Serif,24,-1,5,75,0,0,0,0,0")); qSettings.endGroup(); setParams(); } void editor::writeSettings() { int i; QSettings qSettings(QSettings::Ini); getParams(); qSettings.beginGroup(APP_KEY+"/EDITOR/"); qSettings.writeEntry( "rxWindowWidth", width() ); qSettings.writeEntry( "rxWindowHeight", height() ); qSettings.writeEntry( "rxWindowX", x() ); qSettings.writeEntry( "rxWindowY", y() ); for (i=0;igetColor().rgb())); } qSettings.writeEntry("fgColor",(int)ev->fgbgFrame->getForegroundColor().rgb()); qSettings.writeEntry("bgColor",(int)ev->fgbgFrame->getBackgroundColor().rgb()); qSettings.writeEntry("brushStyle",(int)ev->editorCanvas->getBrushStyle()); qSettings.writeEntry("font",ev->editorCanvas->getFont()); qSettings.endGroup(); } void editor::getParams() { } void editor::setParams() { } void editor::initView() { ev=new editorForm(this); setCentralWidget(ev); // ev->editorCanvas->clear(); connect(ev->arrowPushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotArrow())); connect(ev->rectanglePushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotRectangle())); connect(ev->circlePushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotCircle())); connect(ev->colorpickerPushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotColorpicker())); connect(ev->imagePushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotImage())); connect(ev->linePushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotLine())); connect(ev->penSpinBox,SIGNAL(valueChanged(int)),ev->editorCanvas,SLOT(slotPenWidth(int))); connect(ev->textPushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotText())); connect(ev->fontPushButton,SIGNAL(clicked()),ev->editorCanvas,SLOT(slotSelectFont())); ev->rotateLCD->display( " 0'" ); connect( ev->rotateSlider, SIGNAL(valueChanged(int)),SLOT(slotRotateChanged(int)) ); ev->shearLCD->display( "0.00" ); connect( ev->shearSlider, SIGNAL(valueChanged(int)),SLOT(slotShearChanged(int)) ); connect( ev->textLineEdit, SIGNAL(textChanged(const QString &)),ev->editorCanvas ,SLOT(slotTextReturnPressed(const QString &)) ); connect(ev->editorCanvas,SIGNAL(textLoaded(const QString &)),SLOT(slotTextChanged(const QString &))); connect( ev->editorCanvas, SIGNAL(setShearRotate(double,int)),SLOT(slotShearRotateChanged(double,int)) ); initColorSelectors(); ev->editorCanvas->setEditorForm(ev); slotRotateChanged(0); slotShearChanged(0); } void editor::slotTextChanged(const QString &t) { ev->textLineEdit->setText(t); } void editor::slotRotateChanged(int) { QString tmp; int rot = ev->rotateSlider->value()*5; if ( rot < 0 ) rot = rot + 360; tmp.sprintf( "%3i'", rot ); ev->rotateLCD->display( tmp ); ev->editorCanvas->setRotate(rot); } void editor::slotShearChanged(int) { double shearVal = ((double)ev->shearSlider->value())/25.; QString tmp; tmp.sprintf( "%1.3f", shearVal ); if ( shearVal >= 0 ) tmp.insert( 0, " " ); ev->shearLCD->display( tmp ); ev->editorCanvas->setShear(shearVal); } void editor::slotShearRotateChanged(double s,int r) { QString tmp; ev->rotateSlider->blockSignals(TRUE); ev->shearSlider->blockSignals(TRUE); int rot = r; tmp.sprintf( "%3i'", rot ); ev->rotateLCD->display( tmp ); if ( rot >180) rot -= 360; ev->rotateSlider->setValue(rot/5); tmp.sprintf( "%1.3f", s); if ( s>= 0 ) tmp.insert( 0, " " ); ev->shearLCD->display( tmp ); ev->shearSlider->setValue((int)(s*25.)); ev->rotateSlider->blockSignals(FALSE); ev->shearSlider->blockSignals(FALSE); } void editor::initActions() { fileNew = new QAction(tr("New file"),tr("&New"),0, this); fileNew->setStatusTip(tr("Open a new file")); fileNew->setWhatsThis(tr("Open file\nOpen a new file\n")); connect(fileNew, SIGNAL(activated()), this, SLOT(slotFileNew())); fileOpen = new QAction(tr("Open file"),QPixmap::fromMimeSource("fileopen.png"),tr("&Open"),0, this); fileOpen->setStatusTip(tr("Open an existing file")); fileOpen->setWhatsThis(tr("Open file\nOpen an existing file\n")); connect(fileOpen, SIGNAL(activated()), this, SLOT(slotFileOpen())); fileSave = new QAction(tr("Save file"),tr("Save file .."),0, this); fileSave->setStatusTip(tr("Save the file under the same name and format")); fileSave->setWhatsThis(tr("Save file\nSave the file under the same name and format\n")); connect(fileSave, SIGNAL(activated()), this, SLOT(slotFileSave())); fileSaveImage = new QAction(tr("Save Image file"),tr("Save &Image file .."),0, this); fileSaveImage->setStatusTip(tr("Save the file in PNG format")); fileSaveImage->setWhatsThis(tr("Save file\nSave the file under a new name\n")); connect(fileSaveImage, SIGNAL(activated()), this, SLOT(slotFileSaveImage())); fileSaveTemplate = new QAction(tr("Save template file"),tr("&Save &Template .."),0, this); fileSaveTemplate->setStatusTip(tr("Save template file ")); fileSaveTemplate->setWhatsThis(tr("Save template file\nSave the file as a temlate file\n")); connect(fileSaveTemplate, SIGNAL(activated()), this, SLOT(slotFileSaveTemplate())); fileQuit = new QAction(tr("Quit"), tr("&Quit"), QAccel::stringToKey(tr("Ctrl+Q")), this); fileQuit->setStatusTip(tr("Quits the editor")); fileQuit->setWhatsThis(tr("Quit\n\nQuits the editor")); connect(fileQuit, SIGNAL(activated()), this, SLOT(slotFileQuit())); /* formatFont = new QAction(tr("Font"),QPixmap::fromMimeSource("fonta.xpm"),tr("&Font"), QAccel::stringToKey(tr("Ctrl+F")), this); formatFont->setStatusTip(tr("Select fonts and attributes")); formatFont->setWhatsThis(tr("Font\n\nSelect fonts and attributes")); connect(formatFont, SIGNAL(activated()), ev->editorCanvas, SLOT(slotFormatFont())); setBg=new QAction(tr("Set Background Color"),tr("Set &Bg"), QAccel::stringToKey(tr("Ctrl+B")), this); setBg->setStatusTip(tr("Fills the background with the background color")); setBg->setWhatsThis(tr("Set Background Color\n\nFills the background with the background color")); connect(setBg, SIGNAL(activated()), ev->editorCanvas, SLOT(slotSetBg())); */ copy= new QAction(tr("Copy"),tr("&Copy"), QAccel::stringToKey(tr("Ctrl+C")), this); copy->setStatusTip(tr("Copy object")); copy->setWhatsThis(tr("Copy\n\nCopy object")); connect(copy, SIGNAL(activated()), ev->editorCanvas, SLOT(slotCopy())); paste= new QAction(tr("Paste"),tr("Paste"), QAccel::stringToKey(tr("Ctrl+V")), this); paste->setStatusTip(tr("Paste object")); paste->setWhatsThis(tr("Paste\n\nPaste object")); connect(paste, SIGNAL(activated()), ev->editorCanvas, SLOT(slotPaste())); clearAll= new QAction(tr("Clear All"),tr("Clear &All"), QAccel::stringToKey(tr("Ctrl+A")), this); clearAll->setStatusTip(tr("Delete all objects and fill the background with the background color")); clearAll->setWhatsThis(tr("Clear All\n\nDelete all objects and fill the background with the background color")); connect(clearAll, SIGNAL(activated()), ev->editorCanvas, SLOT(slotClearAll())); } void editor::initToolbar() { toolBar = new QToolBar(this, "editor toolbar"); fileOpen->addTo(toolBar); resizeCanvas=new QComboBox(FALSE,toolBar); resizeCanvas->insertItem("160x120"); resizeCanvas->insertItem("320x240"); resizeCanvas->insertItem("320x256"); resizeCanvas->insertItem("500x400"); resizeCanvas->insertItem("500x496"); resizeCanvas->insertItem("640x496"); resizeCanvas->insertItem("800x616"); resizeCanvas->setCurrentItem(2); connect(resizeCanvas, SIGNAL(activated(int)), SLOT(slotResizeCanvas(int))); // formatFont->addTo(toolBar); } void editor::slotResizeCanvas(int indx) { int w=340; int h=256; switch (indx) { case 0: w=160; h=128; break; case 1: w=320; h=240; break; case 2: w=320; h=256; break; case 3: w=512; h=400; break; case 4: w=512; h=496; break; case 5: w=640; h=496; break; case 6: w=800; h=616; break; default: break; } ev->editorCanvas->setCanvasSize(w,h); } void editor::initMenubar() { fileMenu=new QPopupMenu(); editMenu=new QPopupMenu(); fileNew->addTo(fileMenu); fileOpen->addTo(fileMenu); fileSave->addTo(fileMenu); fileSaveImage->addTo(fileMenu); fileSaveTemplate->addTo(fileMenu); fileQuit->addTo(fileMenu); // formatFont->addTo(toolMenu); // setBg->addTo(toolMenu); copy->addTo(editMenu); paste->addTo(editMenu); clearAll->addTo(editMenu); menuBar()->insertItem(tr("&File"), fileMenu); menuBar()->insertItem(tr("&Edit"), editMenu); } void editor::slotFileNew() { if(ev->editorCanvas->isModified()) { switch( QMessageBox::information( this, "Editor", "The document has not been saved as a template\n", "&Continue Anyway","Cancel",NULL, -1, // Enter == button 0 1 ) ) { // Escape == button 2 case 0: // Continu clicked break; case 1: // Cancel clicked return; break; } } ev->editorCanvas->slotClearAll(); localFile.close(); localFile.setName(""); } void editor::slotFileOpen() { QFileDialog *fd = new QFileDialog(this,0,TRUE); fd->show(); /* dirDialog d(this,0,FALSE); QString s=d.openFileName(txImagePath,"*.png *.gif *.jpg *.templ"); if (s==QString::null) return ; if (s.isEmpty()) return ; localFile.setName(s); ev->editorCanvas->open(localFile); debug("localfile after open =%s",(const char *)localFile.name()); */ } /*! \fn editor::slotFileSave() \brief save file under same name and same type */ void editor::slotFileSave() { if(ev->editorCanvas->getImageType()==mcanvas::FLATIMAGE) { debug("localfile =%s",(const char *)localFile.name()); ev->editorCanvas->save(localFile,FALSE); } else { ev->editorCanvas->save(localFile,TRUE); } } void editor::slotFileSaveImage() { dirDialog d; QString s; s=localFile.name(); if(s.isEmpty()) { s=txImagePath; } s=d.saveFileName(s,"*.png","png"); if (s==QString::null) return ; if (s.isEmpty()) return ; localFile.setName(s); ev->editorCanvas->save(localFile,FALSE); } void editor::slotFileSaveTemplate() { dirDialog d; QString s=localFile.name(); if(s.isEmpty()) { s=txImagePath; } s=d.saveFileName(s,"*.templ","templ"); if (s==QString::null) return ; if (s.isEmpty()) return ; localFile.setName(s); ev->editorCanvas->save(localFile,TRUE); } void editor::slotFileQuit() { close(); } void editor::closeEvent(QCloseEvent *e) { if(ev->editorCanvas->isModified()) { switch( QMessageBox::information( this, "Editor", "The document has not been saved as a template\n", "&Continue Anyway","Cancel",NULL, -1, // Enter == button 0 1 ) ) { // Escape == button 2 case 0: // Continu clicked break; case 1: // Cancel clicked return; break; } } emit imageAvailable(ev->editorCanvas->getImage()); writeSettings(); e->accept(); } /** transers QImage *ima to the canvas */ void editor::setImage(QImage *ima) { ev->editorCanvas->setImage(ima); } void editor::initColorSelectors() { int i; csArray[0]=ev->colorSelector1; csArray[1]=ev->colorSelector2; csArray[2]=ev->colorSelector3; csArray[3]=ev->colorSelector4; csArray[4]=ev->colorSelector5; csArray[5]=ev->colorSelector6; csArray[6]=ev->colorSelector7; csArray[7]=ev->colorSelector8; csArray[8]=ev->colorSelector9; csArray[9]=ev->colorSelector10; csArray[10]=ev->colorSelector11; csArray[11]=ev->colorSelector12; for (i=0;ieditorCanvas,SLOT(slotFgColorChanged(QColor))); connect(csArray[i],SIGNAL(bgColorChanged(QColor)),ev->editorCanvas,SLOT(slotBgColorChanged(QColor))); } } void editor::openFile(QFile &f) { ev->editorCanvas->open(f); localFile.setName(f.name()); }