Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
749 werner 19
#include <QtGui>
3 Werner 20
 
749 werner 21
#include "paintarea.h"
22
 
23
// static
24
QColor PaintObject::background_color = Qt::white;
25
 
698 werner 26
/** @class PaintArea
27
  @ingroup GUI
28
  A small compoenent that acts as the "window" for special drawing.
29
  The PaintArea is embedded in the MainWindow. The PaintArea receives UI events from Qt and,
30
  after some processing emits signals.
31
  */
3 Werner 32
 
33
PaintArea::PaintArea(QWidget *parent)
34
     : QWidget(parent)
35
 {
744 werner 36
     m_bitmap = QImage(this->size(), QImage::Format_ARGB32_Premultiplied);
49 Werner 37
     // enable mouse tracking
38
     this->setMouseTracking(true);
135 Werner 39
     // enable keyboard focus
40
     setFocusPolicy(Qt::StrongFocus);
21 Werner 41
 
3 Werner 42
 }
43
 
21 Werner 44
void PaintArea::resizeEvent(QResizeEvent *event)
45
{
780 werner 46
    Q_UNUSED(event);
744 werner 47
    m_bitmap = QImage(this->size(), QImage::Format_ARGB32_Premultiplied);
550 werner 48
    //qDebug() << "paintarea resize" << this->size();
21 Werner 49
}
3 Werner 50
void PaintArea::paintEvent(QPaintEvent *)
51
{
52
 
21 Werner 53
    QPainter painter(this);
54
    QPainter pxpainter(&m_bitmap);
55
 
56
     emit needsPainting(pxpainter);
57
     painter.drawImage(rect(), m_bitmap);
3 Werner 58
}
6 Werner 59
 
60
void PaintArea::mousePressEvent ( QMouseEvent * event )
61
{
40 Werner 62
    m_lastDown = event->pos();
517 werner 63
    //emit mouseClick(event->pos());
49 Werner 64
}
6 Werner 65
 
49 Werner 66
void PaintArea::mouseMoveEvent( QMouseEvent * event )
67
{
517 werner 68
    if (event->buttons() == Qt::LeftButton)
69
        setCursor(Qt::ClosedHandCursor);
49 Werner 70
    emit mouseMove(event->pos());
71
}
6 Werner 72
 
113 Werner 73
void PaintArea::wheelEvent ( QWheelEvent * event )
74
{
75
    emit mouseWheel(event->pos(), event->delta() / 120);
76
}
77
 
40 Werner 78
void PaintArea::mouseReleaseEvent ( QMouseEvent * event )
79
{
80
    setCursor(Qt::CrossCursor);
113 Werner 81
 
517 werner 82
    if ( (event->pos()-m_lastDown).manhattanLength() > 2) {
113 Werner 83
        emit mouseDrag(m_lastDown, event->pos(), event->button());
517 werner 84
    } else {
85
        // a click event...
86
        emit mouseClick(event->pos());
40 Werner 87
    }
88
}
135 Werner 89
 
90
void PaintArea::keyPressEvent(QKeyEvent *event)
91
{
92
    // emulate mouse actions with the keyboard
93
    QPoint mousepos = this->rect().center();
94
    switch (event->key()) {
95
        case Qt::Key_Plus:
96
        case Qt::Key_PageUp:  // zoom in
97
            emit mouseWheel(mousepos, 1);
98
            break;
99
 
100
        case Qt::Key_PageDown: // zoom out
101
        case Qt::Key_Minus:
102
            emit mouseWheel(mousepos, -1);
103
            break;
104
        // pan with cursor keys
105
        case Qt::Key_Left:
106
            emit mouseDrag(mousepos, mousepos + QPoint(20, 0), Qt::MidButton); break;
107
        case Qt::Key_Right:
108
            emit mouseDrag(mousepos, mousepos + QPoint(-20, 0), Qt::MidButton); break;
109
 
110
        case Qt::Key_Up:
111
            emit mouseDrag(mousepos, mousepos + QPoint(0, 20), Qt::MidButton); break;
112
        case Qt::Key_Down:
113
            emit mouseDrag(mousepos, mousepos + QPoint(0, -20), Qt::MidButton); break;
114
        }
115
}