QTfrontend/mapContainer.cpp
author unc0rr
Thu, 05 Oct 2006 14:19:46 +0000
changeset 182 0ca777e942d6
parent 172 5294ada3910b
child 183 57c2ef19f719
permissions -rw-r--r--
Preview with gradient, get rid of workaround with memory buffer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
182
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     1
/*
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     2
 * Hedgewars, a worms-like game
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     3
 * Copyright (c) 2006 Igor Ulyanov <iulyanov@gmail.com>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     4
 *
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     6
 *
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
     9
 * with the Software without restriction, including without limitation the
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    12
 * furnished to do so, subject to the following conditions:
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    13
 *
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    15
 *    this list of conditions and the following disclaimer.
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    18
 *    and/or other materials provided with the distribution.
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    20
 *    derived from this software without specific prior written permission.
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    21
 *
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    32
 */
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    33
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    34
#include "mapContainer.h"
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    35
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    36
#include <QPushButton>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    37
#include <QBuffer>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    38
#include <QUuid>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    39
#include <QBitmap>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    40
#include <QPainter>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    41
#include <QLinearGradient>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    42
#include <QColor>
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    43
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    44
HWMapContainer::HWMapContainer(QWidget * parent) :
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    45
  QWidget(parent), mainLayout(this)
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    46
{
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    47
  imageButt=new QPushButton(this);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    48
  imageButt->setMaximumSize(256, 128);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    49
  imageButt->setFlat(true);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    50
  imageButt->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    51
  mainLayout.addWidget(imageButt);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    52
  connect(imageButt, SIGNAL(clicked()), this, SLOT(changeImage()));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    53
  changeImage();
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    54
}
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    55
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    56
void HWMapContainer::setImage(const QImage newImage)
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    57
{
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    58
  QPixmap px(256, 128);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    59
  QPixmap pxres(256, 128);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    60
  QPainter p(&pxres);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    61
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    62
  px.fill(Qt::yellow);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    63
  QBitmap bm = QBitmap::fromImage(newImage);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    64
  px.setMask(bm);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    65
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    66
  QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    67
  linearGrad.setColorAt(0, QColor(0, 0, 192));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    68
  linearGrad.setColorAt(1, QColor(0, 0, 64));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    69
  p.fillRect(QRect(0, 0, 256, 128), linearGrad);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    70
  p.drawPixmap(QPoint(0, 0), px);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    71
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    72
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    73
  imageButt->setIcon(pxres);
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    74
  imageButt->setIconSize(QSize(256, 128));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    75
}
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    76
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    77
void HWMapContainer::changeImage()
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    78
{
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    79
  pMap=new HWMap();
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    80
  connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    81
  m_seed = QUuid::createUuid().toString();
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    82
  pMap->getImage(m_seed.toStdString());
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    83
}
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    84
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    85
QString HWMapContainer::getCurrentSeed() const
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    86
{
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    87
  return m_seed;
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    88
}
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    89
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    90
void HWMapContainer::resizeEvent ( QResizeEvent * event )
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    91
{
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    92
  //imageButt->setIconSize(imageButt->size());
0ca777e942d6 Preview with gradient, get rid of workaround with memory buffer
unc0rr
parents: 172
diff changeset
    93
}