Subversion Repositories public iLand

Rev

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

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