QTfrontend/ui/widget/drawmapwidget.cpp
changeset 6060 fdfc01419815
parent 5252 ded882439548
child 6616 f77bb02b669f
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QFile>
       
    20 #include <QMessageBox>
       
    21 #include <QEvent>
       
    22 
       
    23 #include "drawmapwidget.h"
       
    24 
       
    25 DrawMapWidget::DrawMapWidget(QWidget *parent) :
       
    26     QWidget(parent),
       
    27     ui(new Ui::DrawMapWidget)
       
    28 {
       
    29     ui->setupUi(this);
       
    30 
       
    31     m_scene = 0;
       
    32 }
       
    33 
       
    34 DrawMapWidget::~DrawMapWidget()
       
    35 {
       
    36     delete ui;
       
    37 }
       
    38 
       
    39 void DrawMapWidget::changeEvent(QEvent *e)
       
    40 {
       
    41     QWidget::changeEvent(e);
       
    42     switch (e->type()) {
       
    43     case QEvent::LanguageChange:
       
    44         ui->retranslateUi(this);
       
    45         break;
       
    46     default:
       
    47         break;
       
    48     }
       
    49 }
       
    50 
       
    51 void DrawMapWidget::setScene(DrawMapScene * scene)
       
    52 {
       
    53     ui->graphicsView->setScene(scene);
       
    54     m_scene = scene;
       
    55 }
       
    56 
       
    57 void DrawMapWidget::resizeEvent(QResizeEvent * event)
       
    58 {
       
    59     Q_UNUSED(event);
       
    60 
       
    61     if(ui->graphicsView && ui->graphicsView->scene())
       
    62         ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
       
    63 }
       
    64 
       
    65 void DrawMapWidget::showEvent(QShowEvent * event)
       
    66 {
       
    67     Q_UNUSED(event);
       
    68 
       
    69     resizeEvent(0);
       
    70 }
       
    71 
       
    72 void DrawMapWidget::undo()
       
    73 {
       
    74     if(m_scene) m_scene->undo();
       
    75 }
       
    76 
       
    77 void DrawMapWidget::clear()
       
    78 {
       
    79     if(m_scene) m_scene->clearMap();
       
    80 }
       
    81 
       
    82 void DrawMapWidget::save(const QString & fileName)
       
    83 {
       
    84     if(m_scene)
       
    85     {
       
    86         QFile file(fileName);
       
    87 
       
    88         if(!file.open(QIODevice::WriteOnly))
       
    89             QMessageBox::warning(this, tr("File error"), tr("Cannot open file '%1' for writing").arg(fileName));
       
    90         else
       
    91             file.write(qCompress(m_scene->encode()).toBase64());
       
    92     }
       
    93 }
       
    94 
       
    95 void DrawMapWidget::load(const QString & fileName)
       
    96 {
       
    97     if(m_scene)
       
    98     {
       
    99         QFile f(fileName);
       
   100 
       
   101         if(!f.open(QIODevice::ReadOnly))
       
   102             QMessageBox::warning(this, tr("File error"), tr("Cannot read file '%1'").arg(fileName));
       
   103         else
       
   104             m_scene->decode(qUncompress(QByteArray::fromBase64(f.readAll())));
       
   105     }
       
   106 }