display layout of label editor
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 5 Dec 2010 11:22:16 +0000 (11:22 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 5 Dec 2010 11:22:16 +0000 (11:22 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@650 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/templates/templatedlg.cpp
src/templates/ticketedit.cpp
src/templates/ticketedit.h

index 3fd5b21..5e70b96 100644 (file)
@@ -29,6 +29,7 @@ MTemplateChoice::MTemplateChoice(const QString&cd,const QString&tname,const QStr
        :sgroup(sg),cachedir(cd)
 {
        setWindowTitle(tr("Chose Template"));
+       setSizeGripEnabled(true);
        QVBoxLayout *vl;
        setLayout(vl=new QVBoxLayout);
        vl->addWidget(new QLabel(tr("Please chose a variant of template %1:").arg(tname)));
@@ -72,6 +73,7 @@ MTemplateEditor::MTemplateEditor(MTemplateStore*s)
        nochange=false;
        
        setWindowTitle(tr("Edit Template Directory"));
+       setSizeGripEnabled(true);
        
        QHBoxLayout*hl;
        QVBoxLayout*vl,*vl2;
@@ -250,46 +252,28 @@ void MTemplateEditor::editItem()
        QString fn=model->data(bidx,NAMEROLE).toString();
        qDebug()<<"editing base"<<base<<"fn"<<fn;
        //check extension
+       MTemplate::Types tp=MTemplate::UnknownTemplate;
+       MTemplate tmp;
        if(fn!=""){
-               MTemplate tmp=store->getTemplateByFile(fn);
-               MTemplate::Type tp=tmp.type();
-               if(tp&MTemplate::OdfTemplateMask){
-                       QMessageBox::warning(this,"Sorry","Sorry, no ODF editor yet.");
-               }else
-               if(tp==MTemplate::LabelTemplate){
-                       MTicketEditor *ed=new MTicketEditor(this);
-                       ed->loadFile(tmp.cacheFileName());
-                       ed->show();
-               }else
-               if(tp==MTemplate::ZipTemplate){
-                       QMessageBox::warning(this,"Sorry","Sorry, no ZIP selection");
-               }else{
-                       QMessageBox::warning(this,tr("Warning"),tr("Unknown template type, cannot edit it."));
-               }
+               tmp=store->getTemplateByFile(fn);
+               tp=tmp.type();
        }else{
+               tp=MTemplate::legalTypes(base);
        }
-       /*QString ext=QFileInfo(fn).completeSuffix();
-       if(!MTemplate::legalSuffixes(base).contains(ext)){
-               QMessageBox::warning(this,tr("Warning"),tr("Files with this extension (%1) are not legal for this template.").arg(ext));
-               return;
-       }
-       //get new variant name
-       QStringList lst;
-       for(int i=0;i<model->rowCount(pidx);i++){
-               QStringList f=model->data(model->index(i,0,pidx),NAMEROLE).toString().split(",");
-               if(f.size()>1)
-                       lst<<f[1];
-       }
-       QString nvar;
-       for(int i=0;i<1000000000;i++){
-               nvar=QString::number(i,16).toLower();
-               if(!lst.contains(nvar))break;
+       if(tp&MTemplate::OdfTemplateMask){
+               QMessageBox::warning(this,"Sorry","Sorry, no ODF editor yet.");
+       }else
+       if(tp==MTemplate::LabelTemplate){
+               MTicketEditor *ed=new MTicketEditor(this);
+               if(fn!="")
+                       ed->loadFile(tmp.cacheFileName());
+               ed->show();
+       }else
+       if(tp==MTemplate::ZipTemplate){
+               QMessageBox::warning(this,"Sorry","Sorry, no ZIP selection");
+       }else{
+               QMessageBox::warning(this,tr("Warning"),tr("Unknown template type, cannot edit it."));
        }
-       //send up
-       if(store->setTemplate(base+"."+ext+","+nvar,fn))
-               forceUpdate();
-       else
-               QMessageBox::warning(this,tr("Warning"),tr("Unable to upload file."));*/
 }
 
 void MTemplateEditor::saveItem()
index b357367..08688ce 100644 (file)
 
 #include "ticketedit.h"
 
+#include<QAction>
+#include<QBoxLayout>
+#include<QComboBox>
+#include<QDomDocument>
+#include<QDomElement>
+#include<QDoubleSpinBox>
+#include<QFileDialog>
+#include<QHeaderView>
+#include<QLabel>
 #include<QMenu>
 #include<QMenuBar>
-#include<QAction>
+#include<QMessageBox>
+#include<QPointF>
+#include<QSizeF>
+#include<QSplitter>
+#include<QStandardItemModel>
 #include<QStatusBar>
-#include<QFileDialog>
+#include<QTableView>
+#include<QUnZip>
+#include<QZip>
+
+class DPTR_CLASS_NAME(MTicketEditor)
+{
+       public:
+               //template file
+               QString mFileName;
+               //file contents
+               QMap<QString,QByteArray>mFiles;
+               QString mUnit;
+               QSizeF mSize;
+               enum Type{
+                       Unknown,
+                       LoadFont,
+                       Picture,
+                       Text,
+                       Barcode
+               };
+               struct Line_s{
+                       Type type;
+                       QSizeF size;
+                       QPointF offset;
+                       QString file;
+                       bool smooth;
+                       QString font;
+                       double fontsize;
+                       Qt::Alignment align;
+               };
+               QList<Line_s>mLines;
+               //widgets
+               QDoubleSpinBox*labelwidth,*labelheight;
+               QComboBox*unitbox;
+               QTableView*itemtable,*filetable;
+               QStandardItemModel*itemmodel,*filemodel;
+               QWidget*labelpicture;
+};
+
+DEFINE_DPTR(MTicketEditor);
 
 MTicketEditor::MTicketEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(parent, f)
 {
@@ -34,32 +86,146 @@ MTicketEditor::MTicketEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(pa
        m=mb->addMenu(tr("&Edit"));
        m->addAction(tr("Add Item"));
        m->addAction(tr("Delete Item"));
+       m->addSeparator();
+       m->addAction(tr("Add File"));
+       m->addAction(tr("Delete File"));
+       
+       //central
+       QSplitter*central=new QSplitter(Qt::Vertical);
+       setCentralWidget(central);
+       QWidget*w=new QWidget;
+       central->addWidget(w);
+       QVBoxLayout*vl;
+       QHBoxLayout*hl;
+       w->setLayout(vl=new QVBoxLayout);
+       vl->addLayout(hl=new QHBoxLayout,0);
+       hl->addWidget(new QLabel(tr("Label Size:")));
+       hl->addWidget(d->labelwidth=new QDoubleSpinBox);
+       hl->addWidget(new QLabel("x"));
+       hl->addWidget(d->labelheight=new QDoubleSpinBox);
+       hl->addWidget(d->unitbox=new QComboBox);
+       d->unitbox->addItem(tr("Millimeter"),"mm");
+       d->unitbox->addItem(tr("Inch"),"in");
+       hl->addStretch(1);
+       vl->addWidget(d->itemtable=new QTableView);
+       d->itemtable->setModel(d->itemmodel=new QStandardItemModel(this));
+       d->itemtable->horizontalHeader()->show();
+       d->itemtable->verticalHeader()->hide();
+       QSplitter*split;
+       central->addWidget(split=new QSplitter);
+       split->addWidget(d->filetable=new QTableView);
+       d->filetable->setModel(d->filemodel=new QStandardItemModel(this));
+       split->addWidget(d->labelpicture=new QWidget);
+       d->filetable->horizontalHeader()->show();
+       d->filetable->verticalHeader()->hide();
        
        //statusbar
        statusBar()->setSizeGripEnabled(true);
        setAttribute(Qt::WA_DeleteOnClose);
 }
 
-void MTicketEditor::loadFile(QString )
+void MTicketEditor::loadFile(QString fn)
+{
+       //try to open the file
+       if(fn=="")return;
+       QFile fd(fn);
+       if(!fd.open(QIODevice::ReadOnly)){
+               QMessageBox::warning(this, tr("Error"), tr("Unable to open file '%1' for reading.").arg(fn));
+               return;
+       }
+       QUnZip unz;
+       if(!unz.open(&fd)){
+               QMessageBox::warning(this, tr("Error"), tr("Unable to interpret file '%1'.").arg(fn));
+               return;
+       }
+       //clear cache
+       d->mFiles.clear();
+       d->mLines.clear();
+       //read data
+       unz.gotoFirstFile();
+       do{
+               QString ifn;
+               unz.getCurrentFileInfo(ifn,0);
+               QBuffer buf;buf.open(QIODevice::ReadWrite);
+               unz.getCurrentFile(buf);
+               if(ifn=="template.xml")
+                       parseTemplate(buf.data());
+               else
+                       d->mFiles.insert(ifn,buf.data());
+       }while(unz.gotoNextFile());
+       //update display
+       updateDisplay();
+}
+
+static inline QSizeF str2size(QString s)
+{
+       QStringList sl=s.trimmed().split(" ");
+       if(sl.size()!=2)return QSizeF();
+       return QSizeF(sl[0].toDouble(),sl[1].toDouble());
+}
+
+static inline QPointF str2point(QString s)
 {
+       QStringList sl=s.trimmed().split(" ");
+       if(sl.size()!=2)return QPointF();
+       return QPointF(sl[0].toDouble(),sl[1].toDouble());
+}
 
+void MTicketEditor::parseTemplate(QByteArray bytes)
+{
+       QDomDocument doc;
+       if(!doc.setContent(bytes)){
+               QMessageBox::warning(this,tr("Warning"),tr("Unable to interpret template data."));
+               return;
+       }
+       QDomElement root=doc.documentElement();
+       d->mSize=str2size(root.attribute("size"));
+       d->mUnit=root.attribute("unit","mm");
+       QDomNodeList nl=root.childNodes();
+       for(int i=0;i<nl.size();i++){
+               QDomElement el=nl.at(i).toElement();
+               if(el.isNull())continue;
+               QString tn=el.tagName();
+               Private::Line_s line;
+               if(tn=="LoadFont")line.type=Private::LoadFont;else
+               if(tn=="Picture")line.type=Private::LoadFont;else
+               if(tn=="Text")line.type=Private::LoadFont;else
+               if(tn=="Barcode")line.type=Private::LoadFont;
+               else line.type=Private::Unknown;
+               line.file=el.attribute("file");
+               line.font=el.attribute("font");
+               line.fontsize=el.attribute("fontsize","10").toDouble();
+               line.offset=str2point(el.attribute("offset"));
+               line.size=str2size(el.attribute("size"));
+               line.smooth=el.attribute("smoot")=="1";
+               line.align=0;
+               tn=el.attribute("align");
+               if(tn=="left")line.align=Qt::AlignLeft;else
+               if(tn=="right")line.align=Qt::AlignRight;else
+               if(tn=="center")line.align=Qt::AlignHCenter;
+               tn=el.attribute("valign");
+               if(tn=="top")line.align|=Qt::AlignTop;else
+               if(tn=="bottom")line.align|=Qt::AlignBottom;else
+               if(tn=="center")line.align|=Qt::AlignVCenter;
+               d->mLines.append(line);
+       }
 }
 
 void MTicketEditor::openFile()
 {
        QString fn=QFileDialog::getOpenFileName(this,tr("Open Ticket Template"));
        if(fn!=""){
-               mFileName=fn;
+               d->mFileName=fn;
                loadFile(fn);
        }
 }
 
 void MTicketEditor::saveFile()
 {
-       if(mFileName.left(2)==":/" || mFileName=="")
+       if(d->mFileName=="" || d->mFileName.left(2)==":/")
                saveFileAs();
        else
-               saveFile(mFileName);
+               saveFile(d->mFileName);
 }
 void MTicketEditor::saveFile ( QString )
 {
@@ -67,9 +233,9 @@ void MTicketEditor::saveFile ( QString )
 }
 void MTicketEditor::saveFileAs()
 {
-       QString fn=QFileDialog::getSaveFileName(this,tr("Save Ticket Template"),mFileName);
+       QString fn=QFileDialog::getSaveFileName(this,tr("Save Ticket Template"),d->mFileName);
        if(fn!=""){
-               mFileName=fn;
+               d->mFileName=fn;
                saveFile(fn);
        }
 }
@@ -82,6 +248,40 @@ void MTicketEditor::upload()
 
 }
 
+void MTicketEditor::updateDisplay()
+{
+       if(d->mFileName=="")
+               setWindowTitle(tr("Label Template Editor"));
+       else
+               setWindowTitle(tr("Label Template Editor [%1]").arg(d->mFileName));
+       //basics
+       d->labelwidth->setValue(d->mSize.width());
+       d->labelheight->setValue(d->mSize.height());
+       for(int i=0;i<d->unitbox->count();i++)
+               if(d->unitbox->itemData(i).toString()==d->mUnit)
+                       d->unitbox->setCurrentIndex(i);
+       //files
+       d->filemodel->clear();
+       if(d->filemodel->columnCount()<2)
+               d->filemodel->insertColumns(0,2);
+       d->filemodel->setHorizontalHeaderLabels(QStringList()<<tr("File Name")<<tr("Size"));
+       //items
+       d->itemmodel->clear();
+       if(d->itemmodel->columnCount()==0)
+               d->itemmodel->insertColumns(0,5);
+       d->itemmodel->setHorizontalHeaderLabels(QStringList()
+               <<tr("Type")
+               <<tr("Size")
+               <<tr("Offset")
+               <<tr("File/Font")
+               <<tr("Font Size")
+               <<tr("smooth")
+               <<tr("Horiz. Alignment")
+               <<tr("Vert. Alignment")
+               );
+       //do rendering
+       rerender();
+}
 
 void MTicketEditor::rerender()
 {
index c2f34a8..1f2878f 100644 (file)
 #define MAGICSMOKE_TICKETEDIT_H
 
 #include <QMainWindow>
+#include <QMap>
+
+#include "dptr.h"
 
 /**An editor for ticket templates.*/
 class MTicketEditor:public QMainWindow
 {
        Q_OBJECT
+       DECLARE_DPTR(d);
        public:
                ///instantiates the editor
                MTicketEditor(QWidget* parent = 0, Qt::WindowFlags f = 0);
@@ -40,8 +44,8 @@ class MTicketEditor:public QMainWindow
                void upload();
        private slots:
                void rerender();
-       private:
-               QString mFileName;
+               void parseTemplate(QByteArray);
+               void updateDisplay();
 };
 
 #endif