File: databaseinfo.h

package info (click to toggle)
chessx 1.4.6-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 11,096 kB
  • sloc: cpp: 56,503; ansic: 2,530; makefile: 7
file content (179 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/***************************************************************************
 *   (C) 2006-2009 Michal Rudolf <[email protected]>                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef __DATABASEINFO_H__
#define __DATABASEINFO_H__

#include "circularbuffer.h"
#include "game.h"

#include <QString>
#include <QObject>
#include <QUndoCommand>
#include <QPointer>
#include <QMetaType>

class Database;
class Filter;
class QUndoStack;
class QUndoGroup;

/** @ingroup Database
The DatabaseInfo class is a simple class to keep various database-related
information together. */


class DatabaseInfo: public QThread
{
    Q_OBJECT
public:
    void run();
    enum {NewGame = -1};
    /** Create information for clipboard database */
    DatabaseInfo(QUndoGroup *undoGroup, Database* db);
    /** Create information for file database */
    DatabaseInfo(QUndoGroup *undoGroup, const QString& filename);
    /** Close database and free memory */
    ~DatabaseInfo();
    /** Open database. */
    bool open(bool utf8);
    /** Close database. */
    void close();
    /** @return @p true if database is valid */
    bool isValid() const
    {
        return m_bLoaded ? m_database != 0 : false;
    }
    /** @return Database object */
    Database* database()
    {
        return m_database;
    }
    const Database* database() const
    {
        return m_database;
    }
    /** @return current filter */
    Filter* filter();
    /** @return current game  */
    Game& currentGame()
    {
        return m_game;
    }
    const Game& currentGame() const
    {
        return m_game;
    }
    /** @return index of current game  */
    int currentIndex() const
    {
        return m_index;
    }
    /** Load game @p index */
    bool loadGame(GameId index);
    /** Starts new game, giving it @p index equal to the number of games in the database */
    void newGame();
    /** Saves game, replacing current one or adding new. If @ref currentIndex() is
    in the database, game will be replaced, else if will be appended at the end. */
    bool saveGame();

    void replaceGame(const Game& game);

    void updateMaterial();

    /** Resizes the filter to the number of games in the database and puts
         *  all games in the filter.*/
    void resetFilter();
    QString displayName() const
    {
        return m_filename;
    }
    QString dbPath() const;

    bool IsLoaded() const;
    bool IsUtf8() const;

    bool IsFicsDB() const;
    bool IsPolyglotBook() const;
    bool IsBook() const;

    static bool IsPolyglotBook(QString name);
    static bool IsBook(QString name);

    bool modified() const;
    bool gameNeedsSaving() const;

    void restoreState(const Game& game);

    QUndoStack *undoStack() const;

    QList<double> m_material;

    const QList<double> &material() const;

    static QString ficsPath();

    bool isClipboard() const;

    const CircularBuffer<int>& lastGames() const { return m_lastGames; }
    void clearLastGames();

protected:
    void doLoadFile(QString filename);

signals:
    void LoadFinished(DatabaseInfo*);
    void signalRestoreState(const Game &game);
    void signalGameModified(bool gameNeedsSaving);
    void signalMoveChanged();
    void searchProgress(int);
    void searchFinished();


public slots:
    void dbCleanChanged(bool);
    void setModified(bool modified, const Game &g, QString action);

private:
    QUndoStack* m_undoStack;
    Database* m_database;
    QPointer<Filter> m_filter;
    Game m_game;
    QString m_filename;
    int m_index;
    CircularBuffer<int> m_lastGames;
    bool m_bLoaded;
    bool m_utf8;
};

Q_DECLARE_METATYPE(DatabaseInfo*)

class GameUndoCommand : public QUndoCommand
{
public:
    GameUndoCommand(QObject* parent, const Game& from, const Game& to, QString action) :
        QUndoCommand(action),
        m_dbInfo((DatabaseInfo*)parent),
        m_fromGame(from),
        m_toGame(to),
        m_bInConstructor(true)
        {
        }

    QPointer<DatabaseInfo> m_dbInfo;
    Game m_fromGame;
    Game m_toGame;
    bool m_bInConstructor;

    virtual void undo() { m_dbInfo->restoreState(m_fromGame); }
    virtual void redo() { if (m_bInConstructor) m_bInConstructor=false; else m_dbInfo->restoreState(m_toGame); }
};

#endif