[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Eliot-dev] eliot/qt board_widget.cpp board_widget.h coord_...
From: |
Olivier Teulière |
Subject: |
[Eliot-dev] eliot/qt board_widget.cpp board_widget.h coord_... |
Date: |
Fri, 22 Oct 2010 16:58:38 +0000 |
CVSROOT: /cvsroot/eliot
Module name: eliot
Changes by: Olivier Teulière <ipkiss> 10/10/22 16:58:38
Modified files:
qt : board_widget.cpp board_widget.h coord_model.cpp
coord_model.h play_word_mediator.cpp
play_word_mediator.h tile_widget.cpp
tile_widget.h
Log message:
Let the TileWidget class handle mouse clicks, instead of doing it in
the BoardWidget class
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/board_widget.cpp?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/board_widget.h?cvsroot=eliot&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/coord_model.cpp?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/coord_model.h?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/play_word_mediator.cpp?cvsroot=eliot&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/play_word_mediator.h?cvsroot=eliot&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/tile_widget.cpp?cvsroot=eliot&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/tile_widget.h?cvsroot=eliot&r1=1.2&r2=1.3
Patches:
Index: board_widget.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/board_widget.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- board_widget.cpp 22 Oct 2010 16:57:59 -0000 1.20
+++ board_widget.cpp 22 Oct 2010 16:58:37 -0000 1.21
@@ -160,9 +160,12 @@
mult = TileWidget::LETTER_TRIPLE;
else if (Board::GetLetterMultiplier(row, col) == 2)
mult = TileWidget::LETTER_DOUBLE;
- TileWidget *t = new TileWidget(this, mult);
+ TileWidget *t = new TileWidget(this, mult, row, col);
m_widgetsMatrix[row][col] = t;
layout->addWidget(t);
+ // Listen to mouse events on the tile
+ connect(t, SIGNAL(mousePressed(int, int, QMouseEvent*)),
+ this, SLOT(tileClicked(int, int, QMouseEvent*)));
}
}
@@ -174,8 +177,8 @@
setMinimumSize(200, 200);
// Listen to changes in the coordinates
- QObject::connect(&m_coordModel, SIGNAL(coordChanged(const Coord&)),
- this, SLOT(updateArrow(const Coord&)));
+ QObject::connect(&m_coordModel, SIGNAL(coordChanged(const Coord&, const
Coord&)),
+ this, SLOT(updateArrow(const Coord&, const Coord&)));
}
@@ -186,11 +189,19 @@
}
-void BoardWidget::updateArrow(const Coord &)
+void BoardWidget::updateArrow(const Coord &iOldCoord, const Coord &iNewCoord)
{
- // Refresh everything
- // We could actually refresh only the 2 involved squares...
- refresh();
+ // Refresh only the 2 involved squares
+ if (iOldCoord.isValid())
+ {
+ TileWidget *t =
m_widgetsMatrix[iOldCoord.getRow()][iOldCoord.getCol()];
+ t->arrowChanged(false, false);
+ }
+ if (iNewCoord.isValid())
+ {
+ TileWidget *t =
m_widgetsMatrix[iNewCoord.getRow()][iNewCoord.getCol()];
+ t->arrowChanged(iNewCoord.isValid(), iNewCoord.getDir() ==
Coord::VERTICAL);
+ }
}
@@ -203,7 +214,6 @@
// tiles (the same performance improvement could be done with caching
// in the TileWidget class, though)
const Board &board = m_game->getBoard();
- const Coord &markCoord = m_coordModel.getCoord();
for (unsigned int row = BOARD_MIN; row <= BOARD_MAX; ++row)
{
for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
@@ -211,10 +221,7 @@
m_widgetsMatrix[row][col]->tileChanged(
board.getTile(row, col),
board.isJoker(row, col),
- board.isTestChar(row, col),
- markCoord.isValid() && markCoord.getRow() == row &&
- markCoord.getCol() == col,
- markCoord.getDir() == Coord::VERTICAL);
+ board.isTestChar(row, col));
}
}
}
@@ -238,7 +245,7 @@
-void BoardWidget::mousePressEvent(QMouseEvent *iEvent)
+void BoardWidget::tileClicked(int row, int col, QMouseEvent *iEvent)
{
if (m_game == NULL)
{
@@ -252,11 +259,6 @@
// - a right click clears any arrow
if (iEvent->button() == Qt::LeftButton)
{
- // Find the coordinates
- const int size = std::min(width(), height());
- const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN
+ 2)));
- int row = iEvent->y() / squareSize;
- int col = iEvent->x() / squareSize;
// Change the direction if this is exactly the same as the current one
Coord coord(row, col, Coord::HORIZONTAL);
if (m_coordModel.getCoord() == coord)
@@ -276,11 +278,6 @@
// - a right click clears any arrow
if (iEvent->button() == Qt::LeftButton)
{
- // Find the coordinates
- const int size = std::min(width(), height());
- const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN
+ 2)));
- int row = iEvent->y() / squareSize;
- int col = iEvent->x() / squareSize;
// Change the direction if this is exactly the same as the current one
Coord coord(row, col, Coord::HORIZONTAL);
if (m_coordModel.getCoord().getRow() == coord.getRow() &&
@@ -307,11 +304,6 @@
// Third version:
// - a left click toggles between horizontal arrow and no arrow
// - a right click toggles between vertical arrow and no arrow
- // Find the coordinates
- const int size = std::min(width(), height());
- const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN +
2)));
- int row = iEvent->y() / squareSize;
- int col = iEvent->x() / squareSize;
if (iEvent->button() == Qt::LeftButton)
{
Coord coord(row, col, Coord::HORIZONTAL);
Index: board_widget.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/board_widget.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- board_widget.h 22 Oct 2010 16:57:59 -0000 1.9
+++ board_widget.h 22 Oct 2010 16:58:37 -0000 1.10
@@ -47,10 +47,11 @@
/// Paint the board
virtual void paintEvent(QPaintEvent *iEvent);
/// Catch mouse clicks on the board
- virtual void mousePressEvent(QMouseEvent *iEvent);
+ //virtual void mousePressEvent(QMouseEvent *iEvent);
private slots:
- void updateArrow(const Coord &iCoord);
+ void tileClicked(int row, int col, QMouseEvent *iEvent);
+ void updateArrow(const Coord &iOldCoord, const Coord &iNewCoord);
private:
/// Encapsulated game, can be NULL
Index: coord_model.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/coord_model.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- coord_model.cpp 19 Feb 2009 18:25:18 -0000 1.1
+++ coord_model.cpp 22 Oct 2010 16:58:37 -0000 1.2
@@ -25,7 +25,7 @@
{
m_prevCoord = m_currCoord;
m_currCoord = iCoord;
- emit coordChanged(iCoord);
+ emit coordChanged(m_prevCoord, iCoord);
}
Index: coord_model.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/coord_model.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- coord_model.h 19 Feb 2009 18:25:18 -0000 1.1
+++ coord_model.h 22 Oct 2010 16:58:37 -0000 1.2
@@ -37,7 +37,7 @@
const Coord &getPrevCoord() const { return m_prevCoord; }
signals:
- void coordChanged(const Coord &iNewCoord);
+ void coordChanged(const Coord &iOldCoord, const Coord &iNewCoord);
private:
Coord m_currCoord;
Index: play_word_mediator.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/play_word_mediator.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- play_word_mediator.cpp 3 Jul 2009 21:40:15 -0000 1.3
+++ play_word_mediator.cpp 22 Oct 2010 16:58:37 -0000 1.4
@@ -89,8 +89,8 @@
this, SLOT(lineEditCoord_returnPressed()));
QObject::connect(&m_pushButtonPlay, SIGNAL(clicked()),
this, SLOT(pushButtonPlay_clicked()));
- QObject::connect(&m_coordModel, SIGNAL(coordChanged(const Coord&)),
- this, SLOT(updateCoord(const Coord&)));
+ QObject::connect(&m_coordModel, SIGNAL(coordChanged(const Coord&, const
Coord&)),
+ this, SLOT(updateCoord(const Coord&, const Coord&)));
}
@@ -204,11 +204,11 @@
}
-void PlayWordMediator::updateCoord(const Coord &iCoord)
+void PlayWordMediator::updateCoord(const Coord &, const Coord &iNewCoord)
{
- if (iCoord.isValid() && m_lineEditCoord.text() != qfw(iCoord.toString()))
- m_lineEditCoord.setText(qfw(iCoord.toString()));
- else if (!iCoord.isValid() && m_lineEditCoord.hasAcceptableInput())
+ if (iNewCoord.isValid() && m_lineEditCoord.text() !=
qfw(iNewCoord.toString()))
+ m_lineEditCoord.setText(qfw(iNewCoord.toString()));
+ else if (!iNewCoord.isValid() && m_lineEditCoord.hasAcceptableInput())
m_lineEditCoord.setText("");
lineEditPlay_textChanged();
}
Index: play_word_mediator.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/play_word_mediator.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- play_word_mediator.h 19 Feb 2009 18:25:19 -0000 1.1
+++ play_word_mediator.h 22 Oct 2010 16:58:37 -0000 1.2
@@ -63,7 +63,7 @@
void lineEditCoord_textChanged(const QString &iText);
void lineEditCoord_returnPressed() { lineEditPlay_returnPressed(); }
void pushButtonPlay_clicked() { lineEditPlay_returnPressed(); }
- void updateCoord(const Coord &iNewCoord);
+ void updateCoord(const Coord &, const Coord &iNewCoord);
private:
PublicGame *m_game;
Index: tile_widget.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/tile_widget.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- tile_widget.cpp 22 Oct 2010 16:57:13 -0000 1.4
+++ tile_widget.cpp 22 Oct 2010 16:58:37 -0000 1.5
@@ -75,12 +75,14 @@
QPainter painter(this);
painter.setFont(letterFont);
- painter.drawText(0, 1, squareSize, squareSize, Qt::AlignCenter, m_text);
+ painter.drawText(1, 1, squareSize, squareSize, Qt::AlignCenter, m_text);
}
-TileWidget::TileWidget(QWidget *parent, Multiplier multiplier)
- : BasicTileWidget(parent), m_multiplier(multiplier), m_isJoker(false),
+TileWidget::TileWidget(QWidget *parent, Multiplier multiplier,
+ int row, int col)
+ : BasicTileWidget(parent), m_multiplier(multiplier),
+ m_row(row), m_col(col), m_isJoker(false),
m_isPreview(false), m_showArrow(false), m_horizontalArrow(true)
{
setMinimumSize(15, 15);
@@ -98,12 +100,17 @@
}
-void TileWidget::tileChanged(const Tile &iTile, bool isJoker, bool isPreview,
- bool showArrow, bool horizontalArrow)
+void TileWidget::tileChanged(const Tile &iTile, bool isJoker, bool isPreview)
{
m_tile = iTile;
m_isJoker = isJoker;
m_isPreview = isPreview;
+ update();
+}
+
+
+void TileWidget::arrowChanged(bool showArrow, bool horizontalArrow)
+{
m_showArrow = showArrow;
m_horizontalArrow = horizontalArrow;
update();
@@ -206,3 +213,9 @@
}
}
+
+void TileWidget::mousePressEvent(QMouseEvent *iEvent)
+{
+ emit mousePressed(m_row, m_col, iEvent);
+}
+
Index: tile_widget.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/tile_widget.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- tile_widget.h 17 Oct 2010 21:45:10 -0000 1.2
+++ tile_widget.h 22 Oct 2010 16:58:37 -0000 1.3
@@ -65,15 +65,21 @@
WORD_TRIPLE = 4
};
- explicit TileWidget(QWidget *parent = 0, Multiplier multiplier = NONE);
+ explicit TileWidget(QWidget *parent = 0, Multiplier multiplier = NONE,
+ int row = 0, int col = 0);
public slots:
- void tileChanged(const Tile &iTile, bool isJoker, bool isPreview,
- bool showArrow, bool horizontalArrow);
+ void tileChanged(const Tile &iTile, bool isJoker, bool isPreview);
+ void arrowChanged(bool showArrow, bool horizontalArrow);
+
+signals:
+ void mousePressed(int row, int col, QMouseEvent *iEvent);
protected:
/// Paint the square
virtual void paintEvent(QPaintEvent *iEvent);
+ /// Catch mouse events to send a signal
+ virtual void mousePressEvent(QMouseEvent *iEvent);
private:
/// Encapsulated tile
@@ -82,6 +88,11 @@
/// Word or letter multipler
Multiplier m_multiplier;
+ /// Position of the tile, only used to fire the mousePressed() signal
+ // XXX: another way would be to send *this in the mousePressed() signal
+ int m_row;
+ int m_col;
+
/// Whether the tile is a joker
bool m_isJoker;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Eliot-dev] eliot/qt board_widget.cpp board_widget.h coord_...,
Olivier Teulière <=