PageRenderTime 79ms CodeModel.GetById 16ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 0ms

/TempatesEditor/trunk/src/view.cpp

http://cupsfilter.googlecode.com/
C++ | 432 lines | 345 code | 57 blank | 30 comment | 16 complexity | 91d86048dd6402e8bfdccdd443fd9550 MD5 | raw file
  1#include "view.h"
  2#include "ui_view.h"
  3
  4
  5#define POINT_TO_CM(cm) ((cm)/28.3465058)
  6#define POINT_TO_MM(mm) ((mm)/2.83465058)     ////////  0.352777778
  7#define POINT_TO_DM(dm) ((dm)/283.465058)
  8#define POINT_TO_INCH(inch) ((inch)/72.0)
  9
 10#define MM_TO_POINT(mm) ((mm)*2.83465058)
 11#define CM_TO_POINT(cm) ((cm)*28.3465058)     ///// 28.346456693
 12#define DM_TO_POINT(dm) ((dm)*283.465058)
 13#define INCH_TO_POINT(inch) ((inch)*72.0)
 14static const int ObjectName = 0;
 15
 16
 17View::View(QWidget *parent) :
 18    QFrame(parent),
 19    m_ui(new Ui::View)
 20{
 21    m_ui->setupUi(this);
 22    init();
 23    setFrameStyle(Sunken | StyledPanel);
 24
 25    //MM_TO_POINT(210)  MM_TO_POINT(297)
 26    createScene();
 27
 28    graphicsView = new QGraphicsView(scene);
 29    graphicsView->setRenderHint(QPainter::Antialiasing, false);
 30    graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
 31    graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
 32    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
 33    graphicsView->setBackgroundBrush(Qt::gray);
 34
 35    int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
 36    QSize iconSize(size, size);
 37
 38    QToolButton *zoomInIcon = new QToolButton;
 39    zoomInIcon->setAutoRepeat(true);
 40    zoomInIcon->setAutoRepeatInterval(33);
 41    zoomInIcon->setAutoRepeatDelay(0);
 42    zoomInIcon->setIcon(QPixmap(":/images/zoomin.png"));
 43    zoomInIcon->setIconSize(iconSize);
 44    QToolButton *zoomOutIcon = new QToolButton;
 45    zoomOutIcon->setAutoRepeat(true);
 46    zoomOutIcon->setAutoRepeatInterval(33);
 47    zoomOutIcon->setAutoRepeatDelay(0);
 48    zoomOutIcon->setIcon(QPixmap(":/images/zoomout.png"));
 49    zoomOutIcon->setIconSize(iconSize);
 50    zoomSlider = new QSlider;
 51    zoomSlider->setMinimum(0);
 52    zoomSlider->setMaximum(500);
 53    zoomSlider->setValue(250);
 54    zoomSlider->setTickPosition(QSlider::TicksRight);
 55
 56    // Zoom slider layout
 57    QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
 58    zoomSliderLayout->addWidget(zoomInIcon);
 59    zoomSliderLayout->addWidget(zoomSlider);
 60    zoomSliderLayout->addWidget(zoomOutIcon);
 61
 62    // Label layout
 63    QHBoxLayout *labelLayout = new QHBoxLayout;
 64    printButton = new QToolButton;
 65    printButton->setIcon(QIcon(QPixmap(":/images/print.png")));
 66    printButton->setIconSize(iconSize);
 67
 68    label = new QLabel();
 69    label->setText(QObject::trUtf8("???????"));
 70    //sys_button =new QRadioButton(QObject::trUtf8("???????????"), this);
 71    //user_button =new QRadioButton(QObject::trUtf8("????????????????"), this);
 72    //sys_button->setChecked(true);
 73    templatesCBox = new QComboBox();
 74
 75    addTmplButton = new QToolButton;
 76    addTmplButton->setIcon(QPixmap(":/images/addtmpl.png"));
 77    addTmplButton->setIconSize(iconSize);
 78
 79    delTmplButton = new QToolButton;
 80    delTmplButton->setIcon(QPixmap(":/images/deltmpl.png"));
 81    delTmplButton->setIconSize(iconSize);
 82
 83    saveTmplButton = new QToolButton;
 84    saveTmplButton->setIcon(QPixmap(":/images/savetmpl.png"));
 85    saveTmplButton->setIconSize(iconSize);
 86
 87    firstPageButton = new QToolButton;
 88    firstPageButton->setIcon(QPixmap(":/images/face_page.png"));
 89    firstPageButton->setIconSize(iconSize);
 90    firstPageButton->setToolTip(QObject::trUtf8("????? ??????? ??????? ???????"));
 91    secondPageButton = new QToolButton;
 92    secondPageButton->setIcon(QPixmap(":/images/over_face_page.png"));
 93    secondPageButton->setToolTip(QObject::trUtf8("????? ???????? ??????? ???????"));
 94    secondPageButton->setIconSize(iconSize);
 95    //label_page = new QLabel();
 96    //label_page->setText(QObject::trUtf8("?????? ????????"));
 97
 98    pageSizeCBox =new QComboBox(this);
 99    //QStringList list;// =QString("A4, A3,B5, Letter, Legal, Executive,A0, A1, A2, A5, A6, A7, A8, A9, B0, B1,B10, B2, B3, B4, B6, B7, B8, B9").split(",");
100    pageSizeCBox->addItems(getPageSizeList());
101
102    QButtonGroup *grpBtn = new QButtonGroup();
103    portretButton = new QToolButton;
104    portretButton->setIcon(QPixmap(":/images/portret.png"));
105    portretButton->setIconSize(iconSize);
106    portretButton->setToolTip(QObject::trUtf8("????? ??????? ?????????? ???????"));
107    portretButton->setCheckable(true);
108    portretButton->setChecked(true);
109
110    landscapeButton = new QToolButton;
111    landscapeButton->setIcon(QPixmap(":/images/landscape.png"));
112    landscapeButton->setIconSize(iconSize);
113    landscapeButton->setToolTip(QObject::trUtf8("????? ????????? ?????????? ???????"));
114    landscapeButton->setCheckable(true);
115
116    grpBtn->addButton(portretButton,0);
117    grpBtn->addButton(landscapeButton,1);
118
119    labelLayout->addWidget(label);
120   // labelLayout->addWidget(sys_button);
121    //labelLayout->addWidget(user_button);
122    labelLayout->addWidget(templatesCBox);
123    labelLayout->addWidget(addTmplButton);
124    labelLayout->addWidget(delTmplButton);
125    labelLayout->addWidget(saveTmplButton);
126    labelLayout->addWidget(firstPageButton);
127    labelLayout->addWidget(secondPageButton);
128    
129    labelLayout->addWidget(pageSizeCBox);
130    labelLayout->addWidget(portretButton);
131    labelLayout->addWidget(landscapeButton);
132
133    labelLayout->addStretch();
134    labelLayout->addWidget(printButton);
135
136
137    //????????? ???????? ???????? ???-???
138    QGridLayout *topLayout = new QGridLayout;
139    topLayout->addLayout(labelLayout, 0, 0);
140    topLayout->addWidget(graphicsView, 1, 0);
141    topLayout->addLayout(zoomSliderLayout, 1, 1);
142    setLayout(topLayout);
143
144    connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
145    connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
146    connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
147    connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
148    connect(pageSizeCBox ,SIGNAL(currentIndexChanged(QString)),this,SLOT(setPaperSize(QString)));
149    connect(grpBtn,SIGNAL(buttonClicked(int)),this,SLOT(flipPage(int)));
150    connect(templatesCBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(readFromStream(QString)));
151    connect(saveTmplButton,SIGNAL(clicked()),this,SLOT(saveToStream()));
152    //QPrintPreviewDialog *p = new QPrintPreviewDialog();
153    //p->show();
154    setupMatrix();
155
156}
157
158
159void View::setPaperSize(const QString & text)
160{
161    qDebug() << Q_FUNC_INFO;
162    QMapIterator<QString, int> i(page_size);
163    while (i.hasNext()) {
164	i.next();
165	if (i.key()==text){
166	    switch (i.value()){
167		case QPrinter::A4:
168		    page_width=210;
169		    page_height=297;
170		    break;
171		case QPrinter::A3:
172		    page_width=297;
173		    page_height=420;
174		    break;
175	    }
176	    qDebug() <<Q_FUNC_INFO << page_height <<page_width;
177
178	}
179    }
180    //drawPage();
181}
182
183bool View::get_pageOrient()
184{
185    return page_orient;
186}
187void View::set_pageOrient(bool orient)
188{
189    page_orient=orient;
190}
191void View::flipPage(int Btn)
192{
193    qDebug() <<Q_FUNC_INFO<< Btn << page_orient <<get_pageOrient()<< page_height <<page_width;
194    qDebug() << margin_left << margin_top << margin_left<<margin_right;
195
196    if (Btn==0){
197	if (page_orient){
198	    page_orient=false;
199	    std::swap(page_height,page_width);
200	    drawPage();
201	}
202    }else{
203	if (Btn==1){
204	    if (!page_orient){
205		page_orient=true;
206		std::swap(page_height,page_width);
207		drawPage();
208	    }
209	}
210
211    }
212}
213
214void View::drawPage()
215{
216    //?????? ????????????? ????????? ??????? (? ???????? ?????)
217
218    qDebug() <<Q_FUNC_INFO<< page_orient << page_height <<page_width;
219    qDebug() << margin_left << margin_top << margin_left<<margin_right;
220
221    scene->clear();
222    scene->setSceneRect(0, 0, MM_TO_POINT(page_width),MM_TO_POINT(page_height));
223    qDebug() << scene->sceneRect();
224
225/*
226    QGraphicsRectItem *paper_rect = new QGraphicsRectItem (QRectF(0,0, MM_TO_POINT(page_width),MM_TO_POINT(page_height)));
227    qDebug() <<"paper_rect"<<MM_TO_POINT(page_width) << MM_TO_POINT(page_height);
228    paper_rect->setPen(QPen(Qt::black));
229    paper_rect->setBrush(QBrush(Qt::white));
230    paper_rect->setZValue(-1000.0);
231    paper_rect->setData(ObjectName, "Paper");
232    scene->addItem(paper_rect);
233*/
234    QGraphicsRectItem *border_rect = new QGraphicsRectItem (QRectF(margin_left, margin_top, MM_TO_POINT(page_width)-margin_left-margin_right,MM_TO_POINT(page_height)-margin_top-margin_bottom));
235    qDebug() <<"margin_rect"<< margin_left << margin_top << margin_left<<margin_right;
236    border_rect->setPen(QPen(Qt::black,2,Qt::DotLine));
237    border_rect->setBrush(QBrush(Qt::white));
238    border_rect->setOpacity(0.5);
239    border_rect->setZValue(0);
240    border_rect->setData(ObjectName, "Border");
241    scene->addItem(border_rect);
242    scene->update();
243
244}
245void View::createScene()
246{
247    scene = new QGraphicsScene();
248    // default page size A4
249    setPaperSize(QString("A4 (210 x 297 mm)"));
250    drawPage();
251
252    for (int i = 0; i < scene->items().size(); ++i){
253	QGraphicsItem *item = scene->items().at(i);
254
255	if ( item->data(ObjectName).toString()=="Paper"){
256	    SimpleItem* pSimpleItem = new SimpleItem;
257	    pSimpleItem->setPos(80, 40);
258	    pSimpleItem->setText(QStringList()<<QObject::trUtf8("????????")<<"   ????13  ");
259	    pSimpleItem->setFlag(QGraphicsItem::ItemIsMovable);
260	    pSimpleItem->setParentItem(item);
261	    //scene->addItem(pSimpleItem);
262
263	    SimpleItem* pSimpleItem1 = new SimpleItem;
264	    pSimpleItem1->setParentItem(item);
265	    pSimpleItem1->setPos(10, 40);
266	    pSimpleItem1->setText(QStringList()<<QObject::trUtf8("?? ?12/23-412")<<"??-4134");
267	    pSimpleItem1->setFlag(QGraphicsItem::ItemIsMovable);
268	    //scene->addItem(pSimpleItem1);
269	}
270    }
271}
272
273
274
275QStringList View::getPageSizeList()
276{
277    QMapIterator<QString, int> i(page_size);
278    QStringList list;
279    while (i.hasNext()) {
280	i.next();
281	list <<i.key();
282    }
283    return list;
284}
285void View::changeEvent(QEvent *e)
286{
287    QFrame::changeEvent(e);
288    switch (e->type()) {
289    case QEvent::LanguageChange:
290        m_ui->retranslateUi(this);
291        break;
292    default:
293        break;
294    }
295}
296
297
298void View::print()
299{
300#ifndef QT_NO_PRINTER
301    QPrinter printer;
302    QPrintDialog dialog(&printer, this);
303    if (dialog.exec() == QDialog::Accepted) {
304	QPainter painter(&printer);
305	QRectF rect;
306	double onepoint = printer.logicalDpiY() / 72.0;
307	scene->render(&painter);
308	//graphicsView->render(&painter);
309    }
310#endif
311}
312
313void View::setupMatrix()
314{
315    qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
316    QMatrix matrix;
317    matrix.scale(scale, scale);
318    graphicsView->setMatrix(matrix);
319
320}
321void View::init()
322{
323    page_orient= false;
324
325    margin_top = MM_TO_POINT(15);
326    margin_bottom=MM_TO_POINT(15);
327    margin_left=MM_TO_POINT(35);
328    margin_right=MM_TO_POINT(15);
329
330    qDebug() <<Q_FUNC_INFO<< page_orient <<margin_left << margin_top << margin_left<<margin_right;
331
332    page_size.insert(QString("A4 (210 x 297 mm)"), QPrinter::A4);
333    page_size.insert(QString("A3 (297 x 420 mm)"), QPrinter::A3);
334    page_size.insert(QString("A0 (841 x 1189 mm)"), QPrinter::A0);
335    page_size.insert(QString("A1 (594 x 841 mm)"), QPrinter::A1);
336    page_size.insert(QString("A2 (420 x 594 mm)"), QPrinter::A2);
337    page_size.insert(QString("A5 (148 x 210 mm)"), QPrinter::A5);
338    page_size.insert(QString("A6 (105 x 148 mm)"), QPrinter::A6);
339    page_size.insert(QString("A7 (74 x 105 mm)"), QPrinter::A7);
340    page_size.insert(QString("A8 (52 x 74 mm)"), QPrinter::A8);
341    page_size.insert(QString("A9 (37 x 52 mm)"), QPrinter::A9);
342    page_size.insert(QString("B0 (1000 x 1414 mm)"), QPrinter::B0);
343    page_size.insert(QString("B1 (707 x 1000 mm)"), QPrinter::B1);
344    page_size.insert(QString("B2 (500 x 707 mm)"), QPrinter::B2);
345    page_size.insert(QString("B3 (353 x 500 mm)"), QPrinter::B3);
346    page_size.insert(QString("B4 (250 x 353 mm)"), QPrinter::B4);
347    page_size.insert(QString("B5 (176 x 250 mm)"), QPrinter::B5);
348    page_size.insert(QString("B6 (125 x 176 mm)"), QPrinter::B6);
349    page_size.insert(QString("B7 (88 x 125 mm)"), QPrinter::B7);
350    page_size.insert(QString("B8 (62 x 88 mm)"), QPrinter::B8);
351    page_size.insert(QString("B9 (44 x 62 mm)"), QPrinter::B9);
352    page_size.insert(QString("B10 (31 x 44 mm)"), QPrinter::B10);
353    page_size.insert(QString("C5E (163 x 229 mm)"), QPrinter::C5E);
354    page_size.insert(QString("DLE (110 x 220 mm)"), QPrinter::DLE);
355    page_size.insert(QString("Executive (191 x 254 mm)"), QPrinter::Executive);
356    page_size.insert(QString("Folio (210 x 330 mm)"), QPrinter::Folio);
357    page_size.insert(QString("Ledger (432 x 279 mm)"), QPrinter::Ledger);
358    page_size.insert(QString("Legal (216 x 356 mm)"), QPrinter::Legal);
359    page_size.insert(QString("Letter (216 x 279 mm)"), QPrinter::Letter);
360    page_size.insert(QString("Tabloid (279 x 432 mm)"), QPrinter::Tabloid);
361
362}
363void View::setTemplatesList(QStringList & t_list)
364{
365    templatesCBox->addItems(t_list);
366}
367
368View::~View()
369{
370    delete m_ui;
371}
372void View::zoomIn()
373{
374    zoomSlider->setValue(zoomSlider->value() + 1);
375}
376
377void View::zoomOut()
378{
379    zoomSlider->setValue(zoomSlider->value() - 1);
380}
381
382void View::saveToStream()
383{
384    QString template_name;
385    template_name=templatesCBox->currentText();
386    QFile file(template_name+".tmpl");
387    file.open(QIODevice::WriteOnly);
388    QDataStream out(&file);   // we will serialize the data into the file
389    out.setVersion(QDataStream::Qt_4_5);
390    out << page_height << page_width <<page_orient;
391    out << margin_top << margin_bottom << margin_left << margin_right;
392    out << scene->items().size();
393    QString t_str;
394
395     for (int i = 0; i < scene->items().size(); ++i){
396	 QGraphicsItem *item = scene->items().at(i);
397	 t_str=item->data(ObjectName).toString();
398
399	 if ( t_str=="Paper"||t_str==QString("Border")){
400	     out <<item->pos() <<item->data(ObjectName).toString();
401	 }else{
402	    SimpleItem* item =(SimpleItem* )scene->items().at(i);
403	    out << item->pos() << item->getFont() <<item->getColor() << item->getText();
404	 }
405    }
406    file.close();
407}
408void View::readFromStream(QString template_name)
409{
410    elemList.clear();
411    QFile file(template_name+".tmpl");
412    file.open(QIODevice::ReadOnly);
413    QDataStream in(&file);
414    in.setVersion(QDataStream::Qt_4_5);
415    //TODO save and read magic word !!!
416    int items_count;
417    in >> page_height >> page_width >>page_orient;
418    in >> margin_top >> margin_bottom >> margin_left >> margin_right;
419    in >> items_count;
420    for (int i=0;i <items_count;i++){
421	SimpleItem* pItem = new SimpleItem;
422	QPointF ps;
423	QFont fnt;
424	QColor col;
425	QStringList pList;
426	in >>ps >>fnt >>col >>pList;
427	pItem->setPos(ps);
428	pItem->setText(pList);
429	elemList.append(pItem);
430    }
431    file.close();
432}