Browse Source

First version of the software

bobbyblues 3 years ago
parent
commit
15fa7b9b76
8 changed files with 664 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 85 0
      CMakeLists.txt
  3. 9 0
      data/sample_list.txt
  4. 12 0
      src/main.cpp
  5. 182 0
      src/table.cpp
  6. 61 0
      src/table.h
  7. 251 0
      src/tableplanner.cpp
  8. 61 0
      src/tableplanner.h

+ 3 - 0
.gitignore

@ -28,3 +28,6 @@
28 28
*.out
29 29
*.app
30 30
31
# QtCreator
32
.user
33

+ 85 - 0
CMakeLists.txt

@ -0,0 +1,85 @@
1
cmake_minimum_required(VERSION 2.8.9)
2
PROJECT(TablePlanner)
3
4
# CMake configuration
5
#-------------------------------------------------------------------------------
6
if(CMAKE_COMPILER_IS_GNUCXX)
7
    add_definitions(-std=gnu++0x)
8
    add_definitions(-DCLM_INTEROPERABILITY)
9
    add_definitions(-DCLM_VERBOSE)
10
endif()
11
12
# Qt5 configuration
13
#-------------------------------------------------------------------------------
14
15
# As moc files are generated in the binary dir, tell CMake
16
# to always look for includes there:
17
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
18
19
# To run moc when necessary
20
SET(CMAKE_AUTOMOC ON) 
21
22
23
# We load the desired Qt5 modules
24
FIND_PACKAGE(Qt5Widgets)
25
26
# Include and library directories
27
#-------------------------------------------------------------------------------
28
29
INCLUDE_DIRECTORIES(
30
    src
31
)
32
33
LINK_DIRECTORIES(
34
35
)
36
37
# Definition of the source and headers files for the project
38
#-------------------------------------------------------------------------------
39
40
SET(TP_HEADERS
41
    src/table.h
42
    src/tableplanner.h
43
)
44
45
SET(TP_SOURCES
46
    src/tableplanner.cpp
47
    src/table.cpp
48
)
49
50
SET(TP_DATA
51
    
52
)
53
54
55
# Compilation
56
#-------------------------------------------------------------------------------
57
ADD_EXECUTABLE(
58
    TablePlanner
59
    ${TP_SOURCES}
60
    ${TP_HEADERS}
61
    src/main.cpp
62
)
63
64
TARGET_LINK_LIBRARIES(
65
    TablePlanner
66
)
67
68
# Linking with proper Qt5 modules
69
#-------------------------------------------------------------------------------
70
qt5_use_modules(TablePlanner Widgets)
71
72
# Definition of the files to copy with the executable
73
#-------------------------------------------------------------------------------
74
75
INSTALL(
76
    FILES ${TP_DATA}
77
    DESTINATION ${CMAKE_BINARY_DIR}/data
78
)
79
80
81
82
IF (WIN32)
83
    INSTALL(TARGETS ID RUNTIME DESTINATION ${CMAKE_BINARY_DIR})
84
ENDIF (WIN32)
85

+ 9 - 0
data/sample_list.txt

@ -0,0 +1,9 @@
1
Catherine
2
Pierre
3
Marjorie
4
David
5
Edith
6
Arnaud
7
Marie
8
Eric
9
Paul

+ 12 - 0
src/main.cpp

@ -0,0 +1,12 @@
1
#include <QApplication>
2
#include "tableplanner.h"
3
4
int main(int argc, char ** argv)
5
{
6
    QApplication app(argc, argv);
7
    TablePlanner table_planner;
8
    table_planner.setWindowTitle("Table Planner");
9
    table_planner.show();
10
11
    return app.exec();
12
}

+ 182 - 0
src/table.cpp

@ -0,0 +1,182 @@
1
#include "table.h"
2
3
#include <QInputDialog>
4
#include <QMessageBox>
5
6
///=============================================================================
7
/// Constructors
8
///=============================================================================
9
10
Table::Table(int table_number, TablePlanner *tableplanner, QWidget * parent)
11
    :QWidget(parent)
12
    ,m_tableplanner(tableplanner)
13
    ,m_table_index(table_number)
14
    ,m_nb_names(0)
15
    ,m_size_limit(4)
16
{
17
18
    // Initialization of the widgets
19
    m_button_add.setText("Add");
20
    m_button_clear.setText("Remove table");
21
    m_button_remove.setText("Remove name");
22
    m_button_change_size_limit.setText("Change size");
23
    m_button_rename.setText("Change name");
24
    m_name = QString("Table ") + QString::number(table_number);
25
    m_label.setAlignment(Qt::AlignCenter);
26
27
28
    // Setting up the layout
29
    m_layout.addWidget(&m_label, 0, 0, 1, 1);
30
    m_layout.addWidget(&m_button_add, 0, 1, 1, 1);
31
    m_layout.addWidget(&m_button_clear, 0, 2, 1, 1);
32
    m_layout.addWidget(&m_button_remove, 0, 3, 1, 1);
33
    m_layout.addWidget(&m_list_invited, 1, 0, 4, 3);
34
    m_layout.addWidget(&m_button_change_size_limit, 1, 3, 1, 1);
35
    m_layout.addWidget(&m_button_rename, 2, 3, 1, 1);
36
37
38
   QObject::connect(&m_button_add, SIGNAL(released()), this, SLOT(addName()));
39
   QObject::connect(&m_button_remove, SIGNAL(released()), this, SLOT(removeName()));
40
   QObject::connect(&m_button_change_size_limit, SIGNAL(released()), this, SLOT(changeSize()));
41
   QObject::connect(&m_button_clear, SIGNAL(released()), this, SLOT(removeTable()));
42
   QObject::connect(&m_button_rename, SIGNAL(released()), this, SLOT(changeName()));
43
44
    this->setLayout(&m_layout);
45
46
   updateLabel();
47
}
48
49
Table::~Table()
50
{
51
52
}
53
54
///=============================================================================
55
/// Signals and slots
56
///=============================================================================
57
58
void Table::addName()
59
{
60
    QString name = m_tableplanner->getCurrentName();
61
    if (name.length() > 0)
62
    {
63
        m_list_invited.insertItem(m_nb_names++, name);
64
        m_tableplanner->disableName(name);
65
        m_names.append(name);
66
    }
67
    if (m_names.length() == m_size_limit)
68
    {
69
        m_button_add.setEnabled(false);
70
    }
71
}
72
73
void Table::removeName()
74
{
75
    int row_index = m_list_invited.currentRow();
76
    removeName(row_index);
77
}
78
79
80
void Table::changeSize()
81
{
82
    bool ok;
83
    int new_size = QInputDialog::getInt(this, tr("New size?"), tr("New table size:"), m_size_limit, 1, 1000, 1, &ok);
84
    if (ok)
85
    {
86
        if (m_names.length() > new_size)
87
        {
88
            QMessageBox msgBox;
89
            msgBox.setText("You need to remove names first.");
90
            msgBox.exec();
91
        }
92
        else
93
        {
94
            m_size_limit = new_size;
95
            if (m_names.length() == m_size_limit)
96
            {
97
                m_button_add.setEnabled(false);
98
            }
99
            else
100
            {
101
                m_button_add.setEnabled(true);
102
            }
103
        }
104
105
    }
106
}
107
108
void Table::removeTable()
109
{
110
    int nb_names = m_names.length();
111
    for (int i = 0; i < nb_names; ++i)
112
    {
113
        removeName(0);
114
    }
115
    m_tableplanner->removeTable(m_table_index);
116
}
117
118
119
void Table::changeName()
120
{
121
    bool ok;
122
    QString text = QInputDialog::getText(this, tr("Change table name"), tr("New table name:"), QLineEdit::Normal, m_name, &ok);
123
    if (ok && !text.isEmpty())
124
    {
125
        m_name = text;
126
        updateLabel();
127
    }
128
}
129
130
///=============================================================================
131
/// Public methods
132
///=============================================================================
133
134
QString Table::getTableName()
135
{
136
    return m_name;
137
}
138
139
QString Table::getNames()
140
{
141
    QString result("");
142
    for (int i = 0; i < m_names.length(); ++i)
143
    {
144
        result += QString(" - ") + QString(m_names[i]) + QString("\n");
145
    }
146
    return result;
147
}
148
149
///=============================================================================
150
/// Private methods
151
///=============================================================================
152
153
void Table::updateList()
154
{
155
    m_list_invited.clear();
156
    for (int i = 0; i < m_names.length(); ++i)
157
    {
158
        m_list_invited.insertItem(i, m_names[i]);
159
    }
160
}
161
162
void Table::removeName(int row_index)
163
{
164
    if (row_index < 0 || row_index >= m_names.length())
165
    {
166
        return;
167
    }
168
    QString name = m_names[row_index];
169
    m_names.remove(row_index);
170
    m_tableplanner->enableName(name);
171
    m_button_add.setEnabled(true);
172
    updateList();
173
}
174
175
void Table::updateLabel()
176
{
177
    QString new_label = m_name + QString(" (") + QString::number(m_size_limit) + QString(")");
178
    m_label.setText(new_label);
179
}
180
181
182

+ 61 - 0
src/table.h

@ -0,0 +1,61 @@
1
#ifndef TABLE_H
2
#define TABLE_H
3
4
#include <QWidget>
5
#include <QGridLayout>
6
#include <QPushButton>
7
#include <QListWidget>
8
#include <QLabel>
9
10
#include<tableplanner.h>
11
12
class TablePlanner;
13
14
class Table : public QWidget
15
{
16
    Q_OBJECT
17
private:
18
    // Gui related
19
    QGridLayout m_layout;
20
    QPushButton m_button_add;
21
    QPushButton m_button_clear;
22
    QPushButton m_button_remove;
23
    QPushButton m_button_change_size_limit;
24
    QPushButton m_button_rename;
25
    QLabel m_label;
26
    QListWidget m_list_invited;
27
    TablePlanner * m_tableplanner;
28
    QVector<QString> m_names;
29
30
    int m_table_index;
31
    int m_size_limit;
32
33
    int m_nb_names;
34
35
    QString m_name;
36
public:
37
    explicit Table(int table_number, TablePlanner * tableplanner, QWidget * parent = 0);
38
    virtual ~Table();
39
40
public slots:
41
    void addName();
42
    void removeName();
43
    void changeSize();
44
    void removeTable();
45
    void changeName();
46
47
public:
48
49
    QString getTableName();
50
    QString getNames();
51
52
53
54
private:
55
    void updateList();
56
    void removeName(int row_index);
57
    void updateLabel();
58
59
60
};
61
#endif//TABLE_H

+ 251 - 0
src/tableplanner.cpp

@ -0,0 +1,251 @@
1
#include "tableplanner.h"
2
3
#include <iostream>
4
#include <QFileDialog>
5
#include <QMessageBox>
6
7
///=============================================================================
8
/// Constructors
9
///=============================================================================
10
11
TablePlanner::TablePlanner(QWidget * parent)
12
    :QWidget(parent)
13
    ,m_current_name_id(-1)
14
{
15
16
    // Initialization of the widgets
17
    m_button_load_persons_list.setText("Load list");
18
    m_button_add_table.setText("Add table");
19
    m_button_output.setText("Output plan");
20
21
    // Setting up the layout
22
    m_layout.addWidget(&m_button_load_persons_list, 0, 0, 1, 1);
23
    m_layout.addWidget(&m_button_add_table, 0, 1, 1, 1);
24
    m_layout.addWidget(&m_button_output, 0, 2, 1, 1);
25
    m_layout.addWidget(&m_list_invited, 1, 0, 10, 3);
26
    m_layout.addWidget(&m_list_table, 1, 4, 10, 4);
27
28
    this->setLayout(&m_layout);
29
30
    // Connecting signals and slots
31
    QObject::connect(&m_button_load_persons_list, SIGNAL(released()), this, SLOT(loadList()));
32
    QObject::connect(&m_button_add_table, SIGNAL(released()), this, SLOT(addTable()));
33
    QObject::connect(&m_list_invited, SIGNAL(currentRowChanged(int)), this, SLOT(updateCurrentNameIndex(int)));
34
    QObject::connect(&m_button_output, SIGNAL(released()), this, SLOT(outputPlan()));
35
36
37
    addTable();
38
39
40
}
41
42
TablePlanner::~TablePlanner()
43
{
44
45
}
46
47
///=============================================================================
48
/// Signals and slots
49
///=============================================================================
50
51
void TablePlanner::loadList()
52
{
53
    QString fn = QFileDialog::getOpenFileName(this,
54
                                              tr("Open File..."),
55
                                              QString(),
56
                                              tr("Text (*.txt);;All Files (*)"));
57
58
    if (!fn.isEmpty())
59
    {
60
        loadPersonsFromFile(fn);
61
        updateList();
62
    }
63
}
64
65
void TablePlanner::addTable()
66
{
67
    Table * new_table = new Table(m_tables.length() + 1, this);
68
    QListWidgetItem * item = new QListWidgetItem();
69
    item->setSizeHint(new_table->sizeHint());
70
    m_table_items.append(item);
71
    m_list_table.addItem(item);;
72
    m_list_table.setItemWidget(item, new_table);
73
    m_tables.append(new_table);
74
}
75
76
void TablePlanner::updateCurrentNameIndex(int new_index)
77
{
78
    m_current_name_id = new_index;
79
}
80
81
82
void TablePlanner::outputPlan()
83
{
84
    QString fn = QFileDialog::getSaveFileName(this,
85
                                              tr("Save as..."),
86
                                              QString(),
87
                                              tr("Text (*.txt);;All Files (*)"));
88
    if (!fn.isEmpty())
89
    {
90
        saveAsText(fn);
91
    }
92
}
93
94
///=============================================================================
95
/// Public methods
96
///=============================================================================
97
98
QString TablePlanner::getCurrentName()
99
{
100
    if (m_current_name_id == -1)
101
    {
102
        return QString("");
103
    }
104
    else
105
    {
106
107
        return getTrueName(m_current_name_id);
108
    }
109
}
110
111
void TablePlanner::disableName(QString name)
112
{
113
    for (int i = 0; i < m_invited_list.length(); ++i)
114
    {
115
        if (name.compare(m_invited_list[i]) == 0)
116
        {
117
            m_valid_names[i] = false;
118
            updateList();
119
            return;
120
        }
121
    }
122
}
123
124
void TablePlanner::enableName(QString name)
125
{
126
    for (int i = 0; i < m_invited_list.length(); ++i)
127
    {
128
        if (name.compare(m_invited_list[i]) == 0)
129
        {
130
            m_valid_names[i] = true;
131
            updateList();
132
            return;
133
        }
134
    }
135
}
136
137
void TablePlanner::removeTable(int index)
138
{
139
    int true_index = index - 1;
140
    if (true_index >= m_tables.length())
141
    {
142
        return;
143
    }
144
145
    m_tables.remove(true_index);
146
    m_table_items.remove(true_index);
147
    updateTableList();
148
149
}
150
151
///=============================================================================
152
/// Private methods
153
///=============================================================================
154
155
void TablePlanner::loadPersonsFromFile(QString filename)
156
{
157
158
    QFile file(filename);
159
    if (!file.open(QIODevice::ReadOnly)) {
160
        QMessageBox msgBox;
161
        msgBox.setText(QString("Error when reading file: \n")  +  file.errorString());
162
        msgBox.exec();
163
        return;
164
    }
165
166
    while (!file.atEnd()) {
167
        QByteArray line = file.readLine();
168
169
        m_invited_list.append(line.split('\n').first());
170
    }
171
172
    file.close();
173
174
175
    // Setting them as valid
176
    m_valid_names.clear();
177
    for (int i = 0; i < m_invited_list.length(); ++i)
178
    {
179
        m_valid_names.append(true);
180
    }
181
182
    m_button_load_persons_list.setEnabled(false);
183
}
184
185
void TablePlanner::updateList()
186
{
187
    m_list_invited.clear();
188
    for (int i = 0; i < m_invited_list.length(); ++i)
189
    {
190
        if (m_valid_names[i])
191
        {
192
            QListWidgetItem * tmp = new QListWidgetItem;
193
            tmp->setText(m_invited_list[i]);
194
            m_list_invited.insertItem(i, tmp);
195
        }
196
    }
197
}
198
199
QString TablePlanner::getTrueName(int index)
200
{
201
    int offset = 0;
202
    for (int i = 0; i < m_invited_list.length(); ++i)
203
    {
204
        if (!m_valid_names[i])
205
        {
206
            offset++;
207
        }
208
        else if (i - offset == index)
209
        {
210
            return m_invited_list[i];
211
        }
212
    }
213
    return "";
214
}
215
216
217
void TablePlanner::updateTableList()
218
{
219
    m_list_table.clear();
220
    for (int i = 0; i < m_tables.length(); ++i)
221
    {
222
        m_list_table.addItem(m_table_items[i]);
223
        m_list_table.setItemWidget(m_table_items[i], m_tables[i]);
224
    }
225
}
226
227
void TablePlanner::saveAsText(QString filename)
228
{
229
    QFile file(filename);
230
    if (!file.open(QIODevice::WriteOnly)) {
231
        QMessageBox msgBox;
232
        msgBox.setText(QString("Error when reading file: \n")  +  file.errorString());
233
        msgBox.exec();
234
        return;
235
    }
236
237
    file.write("Plan: \n");
238
239
    for (int i = 0; i < m_tables.size(); ++i)
240
    {
241
        file.write("\n");
242
        file.write(m_tables[i]->getTableName().toStdString().c_str());
243
        file.write(": \n");
244
        file.write(m_tables[i]->getNames().toStdString().c_str());
245
    }
246
    file.close();
247
248
    QMessageBox msgBox;
249
    msgBox.setText(QString("Plan saved successfully :)"));
250
    msgBox.exec();
251
}

+ 61 - 0
src/tableplanner.h

@ -0,0 +1,61 @@
1
#ifndef TABLEPLANNER_H
2
#define TABLEPLANNER_H
3
4
#include <QWidget>
5
#include <QGridLayout>
6
#include <QSlider>
7
#include <QLabel>
8
#include <QPushButton>
9
#include <QCheckBox>
10
#include <QVector>
11
#include <QString>
12
#include <QListWidget>
13
#include <QListView>
14
15
#include "table.h"
16
17
class Table;
18
19
class TablePlanner : public QWidget
20
{
21
    Q_OBJECT
22
private:
23
    // Gui related
24
    QGridLayout m_layout;
25
    QPushButton m_button_load_persons_list;
26
    QPushButton m_button_add_table;
27
    QPushButton m_button_output;
28
    QListWidget m_list_invited;
29
    QListWidget m_list_table;
30
    QVector<Table *> m_tables;
31
    QVector<QListWidgetItem *> m_table_items;
32
33
    // Data related
34
    QVector<QString> m_invited_list;
35
    QVector<bool> m_valid_names;
36
    int m_current_name_id;
37
38
public:
39
    explicit TablePlanner(QWidget * parent = 0);
40
    virtual ~TablePlanner();
41
42
public slots:
43
    void loadList();
44
    void addTable();
45
    void updateCurrentNameIndex(int new_index);
46
    void outputPlan();
47
48
public:
49
    QString getCurrentName();
50
    void disableName(QString name);
51
    void enableName(QString name);
52
    void removeTable(int index);
53
54
private:
55
    void loadPersonsFromFile(QString filename);
56
    void updateList();
57
    QString getTrueName(int index);
58
    void updateTableList();
59
    void saveAsText(QString filename);
60
};
61
#endif//TABLEPLANNER_H