QTfrontend/drawmapwidget.cpp
author nemo
Fri, 25 Mar 2011 19:25:50 -0400
changeset 5049 96e51a62fb7f
parent 4976 088d40d8aba2
child 5252 ded882439548
permissions -rw-r--r--
Delay portal gun a bit, reduce mudball
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     1
/*
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     2
 * Hedgewars, a free turn based strategy game
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     3
 * Copyright (c) 2011 Andrey Korotaev <unC0Rr@gmail.com>
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    17
 */
088d40d8aba2 Happy 2011 :)
koda
parents: 4586
diff changeset
    18
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    19
#include <QFile>
4586
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    20
#include <QMessageBox>
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    21
4477
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    22
#include "drawmapwidget.h"
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    23
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    24
DrawMapWidget::DrawMapWidget(QWidget *parent) :
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    25
    QWidget(parent),
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    26
    ui(new Ui::DrawMapWidget)
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    27
{
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    28
    ui->setupUi(this);
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    29
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    30
    m_scene = 0;
4477
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    31
}
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    32
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    33
DrawMapWidget::~DrawMapWidget()
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    34
{
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    35
    delete ui;
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    36
}
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    37
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    38
void DrawMapWidget::changeEvent(QEvent *e)
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    39
{
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    40
    QWidget::changeEvent(e);
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    41
    switch (e->type()) {
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    42
    case QEvent::LanguageChange:
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    43
        ui->retranslateUi(this);
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    44
        break;
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    45
    default:
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    46
        break;
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    47
    }
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    48
}
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    49
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    50
void DrawMapWidget::setScene(DrawMapScene * scene)
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    51
{
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    52
    ui->graphicsView->setScene(scene);
4586
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    53
    m_scene = scene;
4477
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    54
}
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    55
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    56
void DrawMapWidget::resizeEvent(QResizeEvent * event)
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    57
{
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    58
    Q_UNUSED(event);
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    59
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    60
    if(ui->graphicsView && ui->graphicsView->scene())
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    61
        ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
63a21fac8bf7 Add QAspectRatioLayout borrowed from http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt to maintain width == height * 2 aspect ratio for map drawing widget
unc0rr
parents:
diff changeset
    62
}
4556
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    63
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    64
void DrawMapWidget::showEvent(QShowEvent * event)
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    65
{
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    66
    Q_UNUSED(event);
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    67
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    68
    resizeEvent(0);
c7f4eb6cbd0c Make use of showEvent, so there's no more need in manual resizing of draw map widget
unc0rr
parents: 4520
diff changeset
    69
}
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    70
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    71
void DrawMapWidget::undo()
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    72
{
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    73
    if(m_scene) m_scene->undo();
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    74
}
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    75
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    76
void DrawMapWidget::clear()
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    77
{
4586
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    78
    if(m_scene) m_scene->clearMap();
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    79
}
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    80
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    81
void DrawMapWidget::save(const QString & fileName)
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    82
{
4586
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    83
    if(m_scene)
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    84
    {
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    85
        QFile file(fileName);
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    86
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    87
        if(!file.open(QIODevice::WriteOnly))
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    88
            QMessageBox::warning(this, tr("File error"), tr("Cannot open file '%1' for writing").arg(fileName));
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    89
        else
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    90
            file.write(qCompress(m_scene->encode()).toBase64());
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    91
    }
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    92
}
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    93
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    94
void DrawMapWidget::load(const QString & fileName)
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
    95
{
4586
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    96
    if(m_scene)
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    97
    {
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    98
        QFile f(fileName);
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
    99
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
   100
        if(!f.open(QIODevice::ReadOnly))
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
   101
            QMessageBox::warning(this, tr("File error"), tr("Cannot read file '%1'").arg(fileName));
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
   102
        else
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
   103
            m_scene->decode(qUncompress(QByteArray::fromBase64(f.readAll())));
4ba4f021070f - Allow user to use undo, to clear, save and load drawn maps
unc0rr
parents: 4560
diff changeset
   104
    }
4560
5d6c7f88db73 - Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents: 4556
diff changeset
   105
}