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

Diff of /queensgui/src/board.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by torben, Thu Jul 19 21:34:15 2007 UTC revision 654 by torben, Thu Apr 22 20:09:09 2010 UTC
# Line 19  Line 19 
19   ***************************************************************************/   ***************************************************************************/
20  #include <qstring.h>  #include <qstring.h>
21  #include <qpainter.h>  #include <qpainter.h>
22  #include <qpopupmenu.h>  #include <q3popupmenu.h>
23    //Added by qt3to4:
24    #include <QMouseEvent>
25    #include <QContextMenuEvent>
26    #include <QPaintEvent>
27    
28  #include "board.h"  #include "board.h"
29    
# Line 27  Line 31 
31  Board::Board(QWidget *parent, const char *name)  Board::Board(QWidget *parent, const char *name)
32   : QWidget(parent, name)   : QWidget(parent, name)
33  {  {
34          m_hasData = false;          m_sol = NULL;
35            m_drawmarkers = false;
36  }  }
37    
38    
39  Board::~Board()  Board::~Board()
40  {  {
41            if (m_sol != NULL)
42                    delete m_sol;
43  }  }
44    
45  void Board::setMatrix(Solution sol)  void Board::setMatrix(Solution* sol)
46  {  {
         m_hasData = true;  
         m_sol = sol;  
         this->repaint(true);  
 }  
47    
48  void Board::setMatrix(IntSolution sol)          if (m_sol != NULL)
49  {          {
50          m_hasData = true;                  delete m_sol;
51          m_sol = Solution();                  m_sol = NULL;
52          for (int i=0;i<m_size;i++)          }
53                  m_sol.matrix[i][ sol.imatrix[i] ] = true;          
54            if (sol != NULL)
55                    m_sol = sol->copy();
56            
57          this->repaint(true);          this->repaint(true);
58  }  }
59    
60    
61    
62  void Board::paintEvent( QPaintEvent *event )  void Board::paintEvent( QPaintEvent *event )
63  {  {
64          QPainter p (this);          QPainter p (this);
# Line 60  void Board::paintEvent( QPaintEvent *eve Line 67  void Board::paintEvent( QPaintEvent *eve
67                  p.drawLine(i*30, 0, i*30, m_size*30);                  p.drawLine(i*30, 0, i*30, m_size*30);
68          }          }
69                    
70          if (m_hasData) {          if (m_sol != NULL) {
71                  QBrush brush( black);                  QBrush brush( Qt::black);
72                  p.setBrush( brush );                  p.setBrush( brush );
73                  for (int i=0; i<m_size; i++)                  for (int i=0; i<m_size; i++)
74                          for (int j=0; j<m_size; j++) {                          for (int j=0; j<m_size; j++) {
75                                  if (m_sol.matrix[i][j])                                  if (m_sol->getMatrix(i,j))
76                                          p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360);                                          p.drawPie( (i*30)+10, (j*30)+10, 10, 10 ,0, 16*360);
77                          }                          }
78          }          }
79            
80    
81            if (m_drawmarkers == true) {
82                    drawMarkerLines();
83                    m_drawmarkers = false;
84            }
85    }
86    
87    void Board::drawMarkerLines()
88    {
89            QPainter p( this );
90            QPen bluePen( Qt::blue );
91            p.setPen( bluePen );
92            int x = m_markerPoint.x() / 30;
93            int y = m_markerPoint.y() / 30;
94            
95            p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) );
96            p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) );
97            
98            QPen redPen( Qt::red );
99            p.setPen( redPen );
100            
101            int startx,stopx;
102            int starty,stopy;
103            starty = stopy = y;
104            startx = stopx = x;
105            
106            // first diagonal line
107            while (startx > 0 && starty > 0) {
108                    startx--;
109                    starty--;
110            }
111            
112            while (stopx < (m_size-1) && stopy < (m_size-1) ) {
113                    stopx++;
114                    stopy++;
115            }
116            p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25));
117            
118            //next diagonal line
119            startx = stopx = x;
120            starty = stopy = y;    
121            while (startx > 0 && starty < (m_size-1) ) {
122                    startx--;
123                    starty++;
124            }
125            while (stopx < (m_size-1) && stopy >0) {
126                    stopx++;
127                    stopy--;
128            }
129            
130            p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5));
131  }  }
132    
133  void Board::mousePressEvent( QMouseEvent *event)  void Board::mousePressEvent( QMouseEvent *event)
134  {  {
135          if (event->button() == LeftButton) {          if (event->button() == Qt::LeftButton) {
136                    m_drawmarkers = true;
137                    m_markerPoint = event->pos();
138                  this->repaint(true);                  this->repaint(true);
                 QPainter p( this );  
                 QPen bluePen( blue );  
                 p.setPen( bluePen );  
                 int x = event->x() / 30;  
                 int y = event->y() / 30;  
           
                 p.drawLine( QPoint(5,(y*30)+15), QPoint( (m_size*30)-5, (y*30)+15) );  
                 p.drawLine( QPoint( (x*30)+15, 5), QPoint( (x*30)+15, (m_size*30)-5 ) );  
           
                 QPen redPen( red );  
                 p.setPen( redPen );  
           
                 int startx,stopx;  
                 int starty,stopy;  
                 starty = stopy = y;  
                 startx = stopx = x;  
           
                 // første skrå linie  
                 while (startx > 0 && starty > 0) {  
                         startx--;  
                         starty--;  
                 }  
           
                 while (stopx < (m_size-1) && stopy < (m_size-1) ) {  
                         stopx++;  
                         stopy++;  
                 }  
                 p.drawLine(QPoint( (startx*30)+5, (starty*30)+5), QPoint( (stopx*30)+25, (stopy*30)+25));  
           
                 //næste skrå linie  
                 startx = stopx = x;  
                 starty = stopy = y;      
                 while (startx > 0 && starty < (m_size-1) ) {  
                         startx--;  
                         starty++;  
                 }  
                 while (stopx < (m_size-1) && stopy >0) {  
                         stopx++;  
                         stopy--;  
                 }  
           
                 p.drawLine(QPoint( (startx*30)+5, (starty*30)+25), QPoint( (stopx*30)+25, (stopy*30)+5));  
139          }          }
140  }  }
141    
142  void Board::setSize( int size )  void Board::setSize( int size )
143  {  {
144          m_hasData = false;          if (m_sol != NULL)
145            {
146                    delete m_sol;
147                    m_sol = NULL;
148            }
149            
150          m_size = size;          m_size = size;
151          this->setMaximumSize( (size * 30)+1, (size * 30)+1 );          this->setMaximumSize( (size * 30)+1, (size * 30)+1 );
152          this->setMinimumSize( size * 30, size * 30);          this->setMinimumSize( size * 30, size * 30);
# Line 130  void Board::setSize( int size ) Line 154  void Board::setSize( int size )
154    
155  void Board::contextMenuEvent( QContextMenuEvent *event)  void Board::contextMenuEvent( QContextMenuEvent *event)
156  {  {
157          QPopupMenu *contextMenu = new QPopupMenu( this );          if (m_sol == NULL)
158                    return;
159            
160            Q3PopupMenu *contextMenu = new Q3PopupMenu( this );
161          contextMenu->setCheckable( false );          contextMenu->setCheckable( false );
162          contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) );          contextMenu->insertItem("Rotate left", this, SLOT( rotateLeft() ) );
163          contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) );          contextMenu->insertItem("Rotate right", this, SLOT( rotateRight() ) );
# Line 143  void Board::contextMenuEvent( QContextMe Line 170  void Board::contextMenuEvent( QContextMe
170    
171  void Board::rotateLeft()  void Board::rotateLeft()
172  {  {
173          m_sol.rotate90();          m_sol->rotate90();
174          repaint( true );          repaint( true );
175  }  }
176    
177  void Board::rotateRight()  void Board::rotateRight()
178  {  {
179          m_sol.rotate90();          m_sol->rotate90();
180          m_sol.rotate90();          m_sol->rotate90();
181          m_sol.rotate90();          m_sol->rotate90();
182          repaint( true );          repaint( true );
183  }  }
184    
185  void Board::mirrorV()  void Board::mirrorV()
186  {  {
187          m_sol.mirror();          m_sol->mirror();
188          repaint( true );          repaint( true );
189  }  }
190    
191  void Board::mirrorH()  void Board::mirrorH()
192  {  {
193          m_sol.mirror();          m_sol->mirror();
194          m_sol.rotate90();          m_sol->rotate90();
195          m_sol.rotate90();          m_sol->rotate90();
196          repaint( true );          repaint( true );
197  }  }

Legend:
Removed from v.1  
changed lines
  Added in v.654

  ViewVC Help
Powered by ViewVC 1.1.20