/[projects]/queensgui/src/board.cpp
ViewVC logotype

Annotation of /queensgui/src/board.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 327 - (hide annotations) (download)
Wed Sep 16 18:53:22 2009 UTC (14 years, 8 months ago) by torben
File size: 4525 byte(s)
Compile with qt 4.5

1 torben 1 /***************************************************************************
2     * Copyright (C) 2005 by Torben Nielsen *
3     * torben@t-hoerup.dk *
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; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This program is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this program; if not, write to the *
17     * Free Software Foundation, Inc., *
18     * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19     ***************************************************************************/
20     #include <qstring.h>
21     #include <qpainter.h>
22 torben 327 #include <q3popupmenu.h>
23     //Added by qt3to4:
24     #include <QMouseEvent>
25     #include <QContextMenuEvent>
26     #include <QPaintEvent>
27 torben 1
28     #include "board.h"
29    
30    
31     Board::Board(QWidget *parent, const char *name)
32     : QWidget(parent, name)
33     {
34 torben 4 m_sol = NULL;
35 torben 1 }
36    
37    
38     Board::~Board()
39     {
40 torben 4 if (m_sol != NULL)
41     delete m_sol;
42 torben 1 }
43    
44 torben 4 void Board::setMatrix(Solution* sol)
45 torben 1 {
46 torben 6
47     if (m_sol != NULL)
48     {
49     delete m_sol;
50     m_sol = NULL;
51     }
52    
53     if (sol != NULL)
54 torben 4 m_sol = sol->copy();
55    
56 torben 1 this->repaint(true);
57     }
58    
59 torben 5
60 torben 1
61     void Board::paintEvent( QPaintEvent *event )
62     {
63     QPainter p (this);
64     for (int i=0;i<(m_size+2);i++) {
65     p.drawLine(0, i*30, m_size*30, i*30);
66     p.drawLine(i*30, 0, i*30, m_size*30);
67     }
68    
69 torben 4 if (m_sol != NULL) {
70 torben 327 QBrush brush( Qt::black);
71 torben 1 p.setBrush( brush );
72     for (int i=0; i<m_size; i++)
73     for (int j=0; j<m_size; j++) {
74 torben 4 if (m_sol->getMatrix(i,j))
75 torben 1 p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360);
76     }
77     }
78     }
79    
80     void Board::mousePressEvent( QMouseEvent *event)
81     {
82 torben 327 if (event->button() == Qt::LeftButton) {
83 torben 1 this->repaint(true);
84     QPainter p( this );
85 torben 327 QPen bluePen( Qt::blue );
86 torben 1 p.setPen( bluePen );
87     int x = event->x() / 30;
88     int y = event->y() / 30;
89    
90     p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) );
91     p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) );
92    
93 torben 327 QPen redPen( Qt::red );
94 torben 1 p.setPen( redPen );
95    
96     int startx,stopx;
97     int starty,stopy;
98     starty = stopy = y;
99     startx = stopx = x;
100    
101 torben 4 // f�rste skr� linie
102 torben 1 while (startx > 0 && starty > 0) {
103     startx--;
104     starty--;
105     }
106    
107     while (stopx < (m_size-1) && stopy < (m_size-1) ) {
108     stopx++;
109     stopy++;
110     }
111     p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25));
112    
113 torben 4 //n�ste skr� linie
114 torben 1 startx = stopx = x;
115     starty = stopy = y;
116     while (startx > 0 && starty < (m_size-1) ) {
117     startx--;
118     starty++;
119     }
120     while (stopx < (m_size-1) && stopy >0) {
121     stopx++;
122     stopy--;
123     }
124    
125     p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5));
126     }
127     }
128    
129     void Board::setSize( int size )
130     {
131 torben 4 if (m_sol != NULL)
132     {
133     delete m_sol;
134     m_sol = NULL;
135     }
136 torben 6
137 torben 1 m_size = size;
138     this->setMaximumSize( (size * 30)+1, (size * 30)+1 );
139     this->setMinimumSize( size * 30, size * 30);
140     }
141    
142     void Board::contextMenuEvent( QContextMenuEvent *event)
143     {
144 torben 4 if (m_sol == NULL)
145     return;
146    
147 torben 327 Q3PopupMenu *contextMenu = new Q3PopupMenu( this );
148 torben 1 contextMenu->setCheckable( false );
149     contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) );
150     contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) );
151     contextMenu->insertItem("Vertical mirror", this, SLOT ( mirrorV() ) );
152     contextMenu->insertItem("Horisontal mirror", this, SLOT( mirrorH() ) );
153    
154     contextMenu->exec( event->globalPos() );
155     delete contextMenu;
156     }
157    
158     void Board::rotateLeft()
159     {
160 torben 4 m_sol->rotate90();
161 torben 1 repaint( true );
162     }
163    
164     void Board::rotateRight()
165     {
166 torben 4 m_sol->rotate90();
167     m_sol->rotate90();
168     m_sol->rotate90();
169 torben 1 repaint( true );
170     }
171    
172     void Board::mirrorV()
173     {
174 torben 4 m_sol->mirror();
175 torben 1 repaint( true );
176     }
177    
178     void Board::mirrorH()
179     {
180 torben 4 m_sol->mirror();
181     m_sol->rotate90();
182     m_sol->rotate90();
183 torben 1 repaint( true );
184     }

Properties

Name Value
svn:eol-style native
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20