File indexing completed on 2024-04-21 15:07:58

0001 // Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com>
0002 //
0003 // This program is free software; you can redistribute it and/or
0004 // modify it under the terms of the GNU General Public License
0005 // version 2 as published by the Free Software Foundation.
0006 //
0007 // This program is distributed in the hope that it will be useful,
0008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0010 // General Public License for more details.
0011 //
0012 // You should have received a copy of the GNU General Public License
0013 // along with this program; see the file COPYING.  If not, write to
0014 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0015 // Boston, MA 02110-1301, USA.
0016 
0017 #ifndef ATLANTIK_ATLANTIK_H
0018 #define ATLANTIK_ATLANTIK_H
0019 
0020 #include <QWidget>
0021 #include <qtextedit.h>
0022 #include <QList>
0023 #include <QAbstractSocket>
0024 #include <QColor>
0025 #include <QPointer>
0026 
0027 #include <kxmlguiwindow.h>
0028 
0029 #include <tokentheme.h>
0030 #include <network_defs.h>
0031 
0032 #include "connectioncookie.h"
0033 
0034 #include <memory>
0035 
0036 class QScrollArea;
0037 class QAction;
0038 class QLineEdit;
0039 class QLabel;
0040 class QVBoxLayout;
0041 class QGridLayout;
0042 class QCommandLineParser;
0043 class AtlanticCore;
0044 class AtlantikNetwork;
0045 class PortfolioView;
0046 class AtlantikBoard;
0047 class Auction;
0048 class ClickableLabel;
0049 
0050 struct AtlantikConfig
0051 {
0052     // General options;
0053     bool chatTimestamps;
0054 
0055     // Personalization options
0056     QString playerName, playerImage;
0057 
0058     // Board options
0059     bool indicateUnowned;
0060     bool highlightUnowned;
0061     bool darkenMortgaged;
0062     bool quartzEffects;
0063     bool animateTokens;
0064 
0065     // Meta server options
0066     bool connectOnStart;
0067     bool hideDevelopmentServers;
0068 
0069     // Portfolio colors
0070     QColor activeColor, inactiveColor;
0071 };
0072 
0073 class EventLog;
0074 class EventLogWidget;
0075 class SelectServer;
0076 class SelectGame;
0077 class SelectConfiguration;
0078 class TradeDisplay;
0079 
0080 class Player;
0081 class Estate;
0082 class Trade;
0083 
0084 class LogTextEdit : public QTextEdit
0085 {
0086 Q_OBJECT
0087 
0088 public:
0089     LogTextEdit(QWidget *parent = nullptr);
0090     virtual ~LogTextEdit();
0091 
0092 protected:
0093     void contextMenuEvent(QContextMenuEvent *event) override;
0094 
0095 private:
0096     QAction *m_clear, *m_selectAll, *m_copy;
0097 };
0098 
0099 /**
0100  * Main Atlantik window.
0101  * Manages gameboard, portfolios and pretty much everything else.
0102  *
0103  * @author Rob Kaper <cap@capsi.com>
0104  */
0105 class Atlantik : public KXmlGuiWindow
0106 {
0107 Q_OBJECT
0108 
0109 public:
0110     /**
0111      * Create an Atlantik window.
0112      *
0113      */
0114     Atlantik(QCommandLineParser *parser = nullptr);
0115 
0116     /**
0117      * Read the configuration settings using KConfig.
0118      *
0119      */
0120     void readConfig();
0121 
0122     AtlantikConfig config() { return m_config; }
0123 
0124 private Q_SLOTS:
0125     void showSelectServer();
0126     void showSelectGame();
0127     void showSelectConfiguration();
0128     void initBoard();
0129     void showBoard();
0130     void freezeBoard();
0131     void clientCookie(const QString &cookie);
0132     void sendHandshake();
0133     void showAboutDialog();
0134 
0135 public Q_SLOTS:
0136 
0137     /**
0138      * A network connection has been established, so we can show the game
0139      * list instead of the server list.
0140      *
0141      */
0142     void slotNetworkConnected();
0143 
0144     /**
0145      * An error occurred while setting up the network connection. Inform the
0146      * user.
0147      *
0148      * @param errno See http://doc.qt.io/qt-4.8/qabstractsocket.html#SocketError-enum
0149      */
0150     void slotNetworkError(QAbstractSocket::SocketError errnum);
0151 
0152     void slotNetworkDisconnected();
0153 
0154     /**
0155      * Creates a new modeless configure dialog or raises it when it already exists.
0156      *
0157      */
0158     void slotConfigure();
0159 
0160     /**
0161      * Opens the event log widget.
0162      *
0163      */
0164     void showEventLog();
0165 
0166     /**
0167      * Opens the KNotify dialog for configuration events.
0168      *
0169      */
0170     void configureNotifications();
0171 
0172     /**
0173      * Reads values from configuration dialog and stores them into
0174      * global configuration struct. If values have changed, appropriate
0175      * methods within the application are called. Configuration is saved
0176      * to file in any case.
0177      *
0178      */
0179     void slotUpdateConfig();
0180 
0181     /**
0182      * Writes the contents of the text input field to the network
0183      * interface and clears the text input field.
0184      *
0185      */
0186     void slotSendMsg();
0187 
0188     /**
0189      * Informs serverMsgs() to append an incoming message from the
0190      * server to the text view as informational message.
0191      *
0192      * @param msg The message to be appended.
0193      */
0194     void slotMsgInfo(const QString &msg);
0195 
0196     void slotMsgStatus(const QString &message, EventType type = ET_Generic);
0197 
0198     /**
0199      * Informs serverMsgs() to append an incoming message from the
0200      * server to the text view as error message.
0201      *
0202      * @param msg The error message to be appended.
0203      */
0204     void slotMsgError(const QString &msg);
0205 
0206     /**
0207      * Informs serverMsgs() to append an incoming message from the
0208      * server to the text view as chat message.
0209      *
0210      * @param player The name of the player chatting.
0211      * @param msg    The chat message to be appended.
0212      */
0213     void slotMsgChat(const QString &player, const QString &msg);
0214 
0215     void slotReconnect();
0216     void slotLeaveGame();
0217 
0218     void newPlayer(Player *player);
0219     void newEstate(Estate *estate);
0220     void newTrade(Trade *trade);
0221     void newAuction(Auction *auction);
0222 
0223     void removeGUI(Player *player);
0224     void removeGUI(Trade *trade);
0225 
0226     void playerChanged(Player *player);
0227     void gainedTurn();
0228 
0229 Q_SIGNALS:
0230     void rollDice();
0231     void buyEstate();
0232     void auctionEstate();
0233     void endTurn();
0234     void jailCard();
0235     void jailPay();
0236     void jailRoll();
0237     void reconnect(const QString &cookie);
0238 
0239 protected:
0240     void closeEvent(QCloseEvent *) override;
0241 
0242 private:
0243     enum MsgType { ErrorMsg, InfoMsg, ChatMsg };
0244     void initNetworkObject();
0245     PortfolioView *addPortfolioView(Player *player);
0246     PortfolioView *findPortfolioView(Player *player);
0247     void appendMsg(const QString &msg, MsgType type);
0248 
0249     QCommandLineParser *m_cliParser;
0250 
0251     QScrollArea *m_portfolioScroll;
0252     QWidget *m_mainWidget, *m_portfolioWidget;
0253     QGridLayout *m_mainLayout;
0254     QVBoxLayout *m_portfolioLayout;
0255     ClickableLabel *m_sbVersion;
0256     ClickableLabel *m_sbStatus;
0257 
0258     QLabel *m_portfolioLabel;
0259     QLineEdit *m_input;
0260     QTextEdit *m_serverMsgs;
0261 
0262     QAction *m_roll, *m_buyEstate, *m_auctionEstate, *m_endTurn,
0263         *m_jailCard, *m_jailPay, *m_jailRoll, *m_configure,
0264         *m_showEventLog, *m_reconnect, *m_leaveGame;
0265 
0266     AtlanticCore *m_atlanticCore;
0267     AtlantikNetwork *m_atlantikNetwork;
0268     AtlantikConfig m_config;
0269 
0270     AtlantikBoard *m_board;
0271     SelectServer *m_selectServer;
0272     SelectGame *m_selectGame;
0273     SelectConfiguration *m_selectConfiguration;
0274     EventLog *m_eventLog;
0275     QPointer<EventLogWidget> m_eventLogWidget;
0276 
0277     QList<PortfolioView *> m_portfolioViews;
0278     QMap<Trade *, TradeDisplay *> m_tradeGUIMap;
0279 
0280     bool m_runningGame;
0281 
0282     std::unique_ptr<ConnectionCookie> m_cookie;
0283     std::unique_ptr<ConnectionCookie> m_reconnectCookie;
0284     bool m_reconnecting;
0285 
0286     TokenTheme m_tokenTheme;
0287 };
0288 
0289 #endif