File: situationwidget.cpp

package info (click to toggle)
boats 201307-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,424 kB
  • ctags: 1,486
  • sloc: cpp: 10,139; makefile: 13; xml: 10
file content (394 lines) | stat: -rw-r--r-- 13,845 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// C++ Implementation: situationwidget
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2008-2011 Thibaut GRIDEL
//
// 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 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
#include <iostream>

#include <QtGui>

#include "situationwidget.h"
#include "trackdelegate.h"
#include "winddelegate.h"

#include "situationmodel.h"

#include "undocommands.h"

extern int debugLevel;

SituationWidget::SituationWidget(QWidget *parent)
    : QTabWidget(parent),
    m_situation(0) {

    // Scenario layout
    scenarioFrame = new QFrame();
    scenarioLayout = new QVBoxLayout(scenarioFrame);

    // Options layout
    optionsGroup = new QGroupBox(scenarioFrame);
    optionsGroup->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);

    optionsForm = new QFormLayout(optionsGroup);

    seriesCombo = new QComboBox(optionsGroup);
    seriesLabel = new QLabel(optionsGroup);
    optionsForm->addRow(seriesLabel, seriesCombo);

    laylineCheck = new QCheckBox(optionsGroup);
    laylineCheckLabel = new QLabel(optionsGroup);
    optionsForm->addRow(laylineCheckLabel, laylineCheck);

    laylineSpin = new QSpinBox(optionsGroup);
    laylineSpinLabel = new QLabel(optionsGroup);
    optionsForm->addRow(laylineSpinLabel, laylineSpin);

    lengthSpin = new QSpinBox(optionsGroup);
    lengthSpinLabel = new QLabel(optionsGroup);
    optionsForm->addRow(lengthSpinLabel, lengthSpin);

    windCheck = new QCheckBox(optionsGroup);
    windCheckLabel = new QLabel(optionsGroup);
    optionsForm->addRow(windCheckLabel, windCheck);

    // Track layout
    trackGroup = new QGroupBox(scenarioFrame);
    trackLayout = new QGridLayout(trackGroup);
    trackTableModel = new TrackTableModel(m_situation);
    trackTableView = new QTableView(trackGroup);
    trackTableView->setItemDelegate(new TrackDelegate);
    trackTableView->setEditTriggers(QAbstractItemView::CurrentChanged);
    trackTableView->verticalHeader()->hide();
    trackTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
    trackTableView->horizontalHeader()->setDefaultSectionSize(60);
    trackTableView->horizontalHeader()->setStretchLastSection(true);
    trackTableView->horizontalHeader()->setClickable(false);
    trackLayout->addWidget(trackTableView);

    // Wind layout
    windGroup = new QGroupBox(scenarioFrame);
    windLayout = new QGridLayout(windGroup);
    windTableModel = new WindTableModel(&m_situation->wind());
    windTableView = new QTableView(windGroup);
    windTableView->setItemDelegate(new WindDelegate);
    windTableView->setEditTriggers(QAbstractItemView::CurrentChanged);
    windTableView->verticalHeader()->hide();
    windTableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    windTableView->horizontalHeader()->setClickable(false);
    windLayout->addWidget(windTableView);


    // last bricks
    scenarioLayout->addWidget(optionsGroup);
    scenarioLayout->addWidget(trackGroup);
    scenarioLayout->addWidget(windGroup);

    addTab(scenarioFrame,tr("Scenario"));


    // description group
    descriptionFrame = new QFrame();

    descriptionGrid = new QGridLayout(descriptionFrame);
    descriptionForm = new QFormLayout();
    descriptionGrid->addLayout(descriptionForm, 0, 0);

    titleEdit = new QLineEdit(descriptionFrame);
    titleLabel = new QLabel(descriptionFrame);
    descriptionForm->addRow(titleLabel, titleEdit);

    rulesEdit = new QLineEdit(descriptionFrame);
    rulesLabel = new QLabel(descriptionFrame);
    descriptionForm->addRow(rulesLabel, rulesEdit);

    abstractLabel = new QLabel(descriptionFrame);
    descriptionGrid->addWidget(abstractLabel,1,0);

    abstractEdit = new QPlainTextEdit(descriptionFrame);
    abstractEdit->setUndoRedoEnabled(false);
    abstractEdit->setContextMenuPolicy(Qt::NoContextMenu);
    descriptionGrid->addWidget(abstractEdit,2,0);

    descriptionLabel = new QLabel(descriptionFrame);
    descriptionGrid->addWidget(descriptionLabel,3,0);

    descriptionEdit = new QPlainTextEdit(descriptionFrame);
    descriptionEdit->setUndoRedoEnabled(false);
    descriptionEdit->setContextMenuPolicy(Qt::NoContextMenu);
    descriptionGrid->addWidget(descriptionEdit,4,0);

    addTab(descriptionFrame,tr("Description"));

}

void SituationWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange) {
        optionsGroup->setTitle(tr("Options"));
        seriesLabel->setText(tr("Series"));
        QStringList seriesList = Boats::seriesList();
        for (int i=0; i<seriesList.size(); ++i) {
            seriesCombo->setItemText(i, seriesList[i]);
        }
        laylineCheckLabel->setText(tr("Show Grid"));
        laylineSpinLabel->setText(tr("Layline Angle"));
        lengthSpinLabel->setText(tr("Zone Length"));
        windCheckLabel->setText(tr("Show Wind"));
        trackGroup->setTitle(tr("Tracks"));
        windGroup->setTitle(tr("Wind"));
        setTabText(0, tr("Scenario"));

        titleLabel->setText(tr("Title"));
        rulesLabel->setText(tr("Rules"));
        abstractLabel->setText(tr("Abstract"));
        descriptionLabel->setText(tr("Description"));
        setTabText(1, tr("Description"));
    } else {
        QTabWidget::changeEvent(event);
    }

}

void SituationWidget::update() {
    if (m_situation) {
        titleEdit->setText(m_situation->title());
        rulesEdit->setText(m_situation->rules());
        seriesCombo->setCurrentIndex(m_situation->situationSeries());
        laylineCheck->setChecked(m_situation->showLayline());
        laylineSpin->setValue(m_situation->laylineAngle());
        lengthSpin->setValue(m_situation->situationLength());
        windCheck->setChecked(m_situation->wind().visible());
        windGroup->setVisible(m_situation->wind().visible());
        abstractEdit->setPlainText(m_situation->abstract());
        descriptionEdit->setPlainText(m_situation->description());
    }
}

void SituationWidget::setSituation(SituationModel *situation) {
    if (situation != m_situation) {
        m_situation = situation;

        update();

        // Scenario Group
        connect (titleEdit, SIGNAL(editingFinished()),
                this, SLOT(setTitle()));
        connect (situation, SIGNAL(titleChanged(QString)),
                titleEdit, SLOT(setText(QString)));

        connect (rulesEdit, SIGNAL(editingFinished()),
                this, SLOT(setRules()));
        connect (situation, SIGNAL(rulesChanged(QString)),
                rulesEdit, SLOT(setText(QString)));

        seriesCombo->addItems(Boats::seriesList());
        connect (seriesCombo, SIGNAL(currentIndexChanged(int)),
                this, SLOT(setSeries(int)));
        connect (situation, SIGNAL(seriesChanged(int)),
                seriesCombo, SLOT(setCurrentIndex(int)));

        connect(laylineCheck, SIGNAL(toggled(bool)),
                this, SLOT(setShowLayline(bool)));
        connect(situation, SIGNAL(showLaylineChanged(bool)),
                laylineCheck, SLOT(setChecked(bool)));

        laylineSpin->setRange(0, 359);
        laylineSpin->setWrapping(true);
        connect (laylineSpin, SIGNAL(valueChanged(int)),
                this, SLOT(setLayline(int)));
        connect (situation, SIGNAL(laylineChanged(const int)),
                laylineSpin, SLOT(setValue(int)));

        lengthSpin->setRange(1,5);
        connect (lengthSpin, SIGNAL(valueChanged(int)),
                this, SLOT(setLength(int)));
        connect (situation, SIGNAL(lengthChanged(const int)),
                lengthSpin, SLOT(setValue(int)));

        connect(windCheck, SIGNAL(toggled(bool)),
                this, SLOT(setShowWind(bool)));
        connect(&situation->wind(), SIGNAL(windVisibleChanged(bool)),
                windCheck, SLOT(setChecked(bool)));

        connect(windCheck, SIGNAL(toggled(bool)),
                windGroup, SLOT(setVisible(bool)));

        connect(abstractEdit->document(), SIGNAL(contentsChanged()),
                this, SLOT(setAbstract()));
        connect(situation, SIGNAL(abstractChanged(const QString)),
                this, SLOT(updateAbstract(const QString)));

        connect(descriptionEdit->document(), SIGNAL(contentsChanged()),
                this, SLOT(setDescription()));
        connect(situation, SIGNAL(descriptionChanged(const QString)),
                this, SLOT(updateDescription(const QString)));

        // Track group
        trackTableModel->setSituation(m_situation);
        connect(situation, SIGNAL(trackAdded(TrackModel*)),
                trackTableModel, SLOT(addTrack(TrackModel*)));
        connect(situation, SIGNAL(trackRemoved(TrackModel*)),
                trackTableModel, SLOT(deleteTrack(TrackModel*)));
        trackTableView->setModel(trackTableModel);

        // Wind group
        windTableModel->setWind(&m_situation->wind());
        connect(&m_situation->wind(), SIGNAL(windReset()),
                windTableModel, SLOT(updateWind()));
        windTableView->setModel(windTableModel);
    }
}

void SituationWidget::unSetSituation() {

    // Scenario Group
    disconnect(titleEdit, 0, 0, 0);
    titleEdit->clear();

    disconnect(rulesEdit, 0, 0, 0);
    rulesEdit->clear();

    disconnect(seriesCombo, 0, 0, 0);
    disconnect(m_situation, 0, seriesCombo, 0);
    seriesCombo->clear();
    seriesCombo->setCurrentIndex(0);

    disconnect(laylineCheck, 0, 0, 0);
    disconnect(m_situation, 0, laylineCheck, 0);
    laylineCheck->setChecked(true);

    disconnect(laylineSpin,  0, 0, 0);
    disconnect(m_situation, 0, laylineSpin, 0);
    laylineSpin->setValue(40);

    disconnect(lengthSpin, 0, 0, 0);
    disconnect(m_situation, 0, lengthSpin, 0);
    lengthSpin->setValue(3);

    disconnect(windCheck, 0, 0, 0);
    disconnect(&m_situation->wind(), 0, windCheck, 0);
    windCheck->setChecked(false);

    disconnect(m_situation, 0, this, 0);
    disconnect(abstractEdit->document(), 0, 0, 0);
    abstractEdit->clear();
    disconnect(descriptionEdit->document(), 0, 0, 0);
    descriptionEdit->clear();

    // Track Group
    disconnect(m_situation, 0, trackTableModel, 0);
    disconnect(&m_situation->wind(), 0, windTableModel, 0);

    m_situation = 0;
}

void SituationWidget::setTitle() {
    if (m_situation) {
        if (titleEdit->text() != m_situation->title()) {
            m_situation->undoStack()->push(new SetTitleUndoCommand(m_situation, titleEdit->text()));
        }
    }
}

void SituationWidget::setRules() {
    if (m_situation) {
        if (rulesEdit->text() != m_situation->rules()) {
            m_situation->undoStack()->push(new SetRulesUndoCommand(m_situation, rulesEdit->text()));
        }
    }
}

void SituationWidget::setShowLayline(bool show) {
    if (m_situation) {
        if (show != m_situation->showLayline())
            m_situation->undoStack()->push(new SetShowLaylineUndoCommand(m_situation));
    }
}

void SituationWidget::setLayline(int angle) {
    if (m_situation) {
        if (angle != m_situation->laylineAngle()) {
            m_situation->undoStack()->push(new SetLaylineUndoCommand(m_situation, angle));
        }
    }
}

void SituationWidget::setLength(int length) {
    if (m_situation) {
        if (length != m_situation->situationLength()) {
            m_situation->undoStack()->push(new LengthMarkUndoCommand(m_situation, length));
        }
    }
}

void SituationWidget::setShowWind(bool show) {
    if (m_situation) {
        if (show != m_situation->wind().visible())
            m_situation->undoStack()->push(new SetShowWindUndoCommand(&m_situation->wind()));
    }
}

void SituationWidget::setSeries(int series) {
    if (m_situation) {
        if (series != m_situation->situationSeries()) {
            m_situation->undoStack()->push(new SetSituationSeriesUndoCommand(m_situation, series));
        }
    }
}

void SituationWidget::setAbstract() {
    if (m_situation) {
        if (abstractEdit->document()->toPlainText() != m_situation->abstract()) {
            m_situation->undoStack()->push(new SetAbstractUndoCommand(m_situation, abstractEdit->document()->toPlainText()));
        }
    }
}

void SituationWidget::setDescription() {
    if (m_situation) {
        if (descriptionEdit->document()->toPlainText() != m_situation->description()) {
            m_situation->undoStack()->push(new SetDescriptionUndoCommand(m_situation, descriptionEdit->document()->toPlainText()));
        }
    }
}

void SituationWidget::updateAbstract(const QString abstract) {
    if (m_situation) {
        if (abstractEdit->document()->toPlainText() != abstract) {
            disconnect(abstractEdit->document(), 0, 0, 0);
            abstractEdit->document()->setPlainText(abstract);
            connect(abstractEdit->document(), SIGNAL(contentsChanged()),
                    this, SLOT(setAbstract()));
        }
    }
}

void SituationWidget::updateDescription(const QString description) {
    if (m_situation) {
        if (descriptionEdit->document()->toPlainText() != description) {
            disconnect(descriptionEdit->document(), 0, 0, 0);
            descriptionEdit->document()->setPlainText(description);
            connect(descriptionEdit->document(), SIGNAL(contentsChanged()),
                    this, SLOT(setDescription()));
        }
    }
}