• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
practicesummarycomponent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2007-2009 Frederik Gladhorn <gladhorn@kde.org>
3  ***************************************************************************
4 
5  ***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "practicesummarycomponent.h"
15 #include "prefs.h"
16 
17 #include "parleyactions.h"
18 
19 #include <QTableWidgetItem>
20 #include <QTextDocument>
21 #include <QTextDocumentWriter>
22 #include <QTextCursor>
23 #include <QTextTable>
24 #include <KMessageBox>
25 #include <keduvocexpression.h>
26 #include <KConfigGroup>
27 #include <KActionCollection>
28 #include <KColorScheme>
29 #include <KToolBar>
30 #include <KFileDialog>
31 
32 using namespace Practice;
33 
34 class PracticeSummaryComponent::SortedAttemptTableWidgetItem: public QTableWidgetItem
35 {
36  virtual bool operator<(const QTableWidgetItem &other) const {
37  if (data(Qt::DisplayRole).toInt() == other.data(Qt::DisplayRole).toInt()) {
38  return data(Qt::UserRole).toInt() < other.data(Qt::UserRole).toInt();
39  }
40  return data(Qt::DisplayRole).toInt() < other.data(Qt::DisplayRole).toInt();
41  }
42 };
43 
44 PracticeSummaryComponent::PracticeSummaryComponent(SessionManagerBase* sessionManager, QWidget* parent)
45  : KXmlGuiWindow(parent)
46  , m_sessionManager(sessionManager)
47 {
48  // KXmlGui
49  setXMLFile("practicesummaryui.rc");
50  setObjectName("Statistics");
51 
52  QWidget *mainWidget = new QWidget(this);
53  setupUi(mainWidget);
54  setCentralWidget(mainWidget);
55 
56  initActions(parent);
57 
58  setupDetailsTable();
59  summaryBar->setStatistics(m_sessionManager->statisticTotalCorrectFirstAttempt(), m_sessionManager->statisticTotalWrong(), m_sessionManager->statisticTotalUnanswered());
60 
61  int total = m_sessionManager->statisticTotalCorrectFirstAttempt() + m_sessionManager->statisticTotalWrong();
62  int minutes = m_sessionManager->totalTime() / 60;
63  int seconds = m_sessionManager->totalTime() % 60;
64 
65  testSummaryLabel->setText(i18nc("number of words, minutes, seconds", "You practiced %1 in %2 and %3.",
66  i18np("one word", "%1 words", total),
67  i18np("one minute", "%1 minutes", minutes),
68  i18np("one second", "%1 seconds", seconds)));
69 
70  KConfigGroup cfg(KSharedConfig::openConfig("parleyrc"), objectName());
71  applyMainWindowSettings(cfg);
72 }
73 
74 PracticeSummaryComponent::~PracticeSummaryComponent()
75 {
76  KConfigGroup cfg(KSharedConfig::openConfig("parleyrc"), objectName());
77  saveMainWindowSettings(cfg);
78 }
79 
80 void PracticeSummaryComponent::initActions(QWidget* parleyMainWindow)
81 {
82  ParleyActions::create(ParleyActions::EnterEditMode, parleyMainWindow, SLOT(showEditor()), actionCollection());
83  ParleyActions::create(ParleyActions::StartPractice, parleyMainWindow, SLOT(showPracticeConfiguration()), actionCollection());
84  ParleyActions::create(ParleyActions::ExportPracticeResults, this, SLOT(exportResults()), actionCollection());
85  actionCollection()->action("practice_start")->setText(i18n("Practice Overview"));
86  actionCollection()->action("practice_start")->setToolTip(i18n("Switch to the Practice Overview page"));
87 }
88 
89 void PracticeSummaryComponent::setupDetailsTable()
90 {
91  tableWidget->setRowCount(m_sessionManager->allEntryCount());
92  tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
93 
94  Qt::ItemFlags flags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
95 
96  KColorScheme scheme(QPalette::Active);
97  QPalette correctPalette = QApplication::palette();
98  correctPalette.setColor(QPalette::WindowText, scheme.foreground(KColorScheme::PositiveText).color());
99  correctPalette.setColor(QPalette::Text, scheme.foreground(KColorScheme::PositiveText).color());
100 
101  QPalette wrongPalette = QApplication::palette();
102  wrongPalette.setColor(QPalette::WindowText, scheme.foreground(KColorScheme::NegativeText).color());
103  wrongPalette.setColor(QPalette::Text, scheme.foreground(KColorScheme::NegativeText).color());
104 
105  int i = 0;
106  // TODO headers with languages
107  // TODO some colors, maybe an indicator icon whether the word was right/wrong
108  foreach(TestEntry * entry, m_sessionManager->allTestEntries()) {
109  QTableWidgetItem* itemFrom = new QTableWidgetItem(
110  entry->entry()->translation(entry->TestEntry::languageFrom())->text());
111  QTableWidgetItem* itemTo = new QTableWidgetItem(
112  entry->entry()->translation(entry->languageTo())->text());
113  if (entry->statisticGoodCount() > 0) {
114  itemTo->setForeground(correctPalette.foreground());
115  }
116 
117  QTableWidgetItem* itemUserAnswer = new QTableWidgetItem(
118  entry->userAnswers().join("; "));
119  itemUserAnswer->setForeground(wrongPalette.foreground());
120 
121  SortedAttemptTableWidgetItem* itemAttempts = new SortedAttemptTableWidgetItem();
122  itemAttempts->setData(Qt::DisplayRole, entry->statisticCount());
123  itemAttempts->setData(Qt::UserRole, entry->statisticBadCount());
124  itemAttempts->setTextAlignment(Qt::AlignRight);
125 
126  itemFrom->setFlags(flags);
127  itemTo->setFlags(flags);
128  itemUserAnswer->setFlags(flags);
129  itemAttempts->setFlags(flags);
130 
131  if (entry->correctAtFirstAttempt()) {
132  itemUserAnswer->setIcon(KIcon("dialog-ok-apply"));
133  } else if (entry->statisticGoodCount() > 0) {
134  itemUserAnswer->setIcon(KIcon("task-attempt"));
135  } else if (entry->statisticCount() > 0) {
136  itemUserAnswer->setIcon(KIcon("dialog-error"));
137  } else {
138  itemUserAnswer->setIcon(KIcon("task-attempt"));
139  }
140 
141  tableWidget->setItem(i, 0, itemAttempts);
142  tableWidget->setItem(i, 1, itemFrom);
143  tableWidget->setItem(i, 2, itemTo);
144  tableWidget->setItem(i, 3, itemUserAnswer);
145  ++i;
146  }
147 
148  tableWidget->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);
149  tableWidget->setSortingEnabled(true);
150  tableWidget->sortItems(0, Qt::DescendingOrder);
151 }
152 
153 void PracticeSummaryComponent::exportResults()
154 {
155  QString filter = "*.html|" + i18n("HTML Files") + "\n*.odt|" + i18n("OpenDocument text files");
156  QString fileName = KFileDialog::getSaveFileName(KUrl("kfiledialog:///practice_export"), filter);
157 
158  if (fileName.isEmpty()) {
159  return;
160  }
161 
162  QTextDocument doc;
163  doc.setHtml("<html><head><title>" + i18n("Practice results") + "</title></body></html>");
164  QTextCursor cursor(&doc);
165 
166  cursor.insertHtml("<h1>" + m_sessionManager->title() + "</h1><br />");
167 
168  cursor.insertText(i18n("Answered questions: %1\n", m_sessionManager->allEntryCount()));
169  cursor.insertText(i18n("Correct answers: %1\n", m_sessionManager->statisticTotalCorrectFirstAttempt()));
170  cursor.insertText(i18n("Wrong answers: %1\n", m_sessionManager->statisticTotalWrong()));
171 
172  QTextTableFormat tableFormat;
173  tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
174  tableFormat.setCellPadding(1);
175  tableFormat.setAlignment(Qt::AlignLeft);
176  QTextTable *table = cursor.insertTable(1, 4, tableFormat);
177  table->cellAt(0, 0).firstCursorPosition().insertHtml(i18n("<b>Attempts</b>"));
178  table->cellAt(0, 1).firstCursorPosition().insertHtml(i18n("<b>Question</b>"));
179  table->cellAt(0, 2).firstCursorPosition().insertHtml(i18n("<b>Correct answer</b>"));
180  table->cellAt(0, 3).firstCursorPosition().insertHtml(i18n("<b>Your errors</b>"));
181 
182  foreach(TestEntry * entry, m_sessionManager->allTestEntries()) {
183  table->appendRows(1);
184  int newRow = table->rows() - 1;
185  table->cellAt(newRow, 0).firstCursorPosition().insertText(QString::number(entry->statisticCount()));
186  table->cellAt(newRow, 1).firstCursorPosition().insertText(entry->entry()->translation(entry->languageFrom())->text());
187  table->cellAt(newRow, 2).firstCursorPosition().insertText(entry->entry()->translation(entry->languageTo())->text());
188  table->cellAt(newRow, 3).firstCursorPosition().insertText(entry->userAnswers().join("; "));
189  }
190 
191  QTextDocumentWriter writer(fileName);
192 
193  if (!writer.write(&doc)) {
194  KMessageBox::error(this, i18n("Could not write to %1", fileName),
195  i18n("Could not write file"));
196  return;
197  }
198 }
199 
200 #include "practicesummarycomponent.moc"
201 
TestEntry::statisticCount
int statisticCount()
Definition: testentry.cpp:49
Practice::PracticeSummaryComponent::~PracticeSummaryComponent
~PracticeSummaryComponent()
Definition: practicesummarycomponent.cpp:74
QTextTable
QWidget
TestEntry::entry
KEduVocExpression * entry() const
Definition: testentry.cpp:149
QTextTableCell::firstCursorPosition
QTextCursor firstCursorPosition() const
ParleyActions::create
KAction * create(ParleyAction id, const QObject *recvr, const char *slot, QObject *parent)
Definition: parleyactions.cpp:64
QTextCursor
QTextTable::rows
int rows() const
Practice::SessionManagerBase::statisticTotalUnanswered
int statisticTotalUnanswered()
Definition: sessionmanagerbase.cpp:218
QTableWidgetItem
QPalette::setColor
void setColor(ColorGroup group, ColorRole role, const QColor &color)
QTextCursor::insertHtml
void insertHtml(const QString &html)
QTextTableFormat
Practice::SessionManagerBase::title
QString title() const
Retun the title of the document.
Definition: sessionmanagerbase.cpp:101
TestEntry::statisticGoodCount
int statisticGoodCount()
Definition: testentry.cpp:59
prefs.h
QStringList::join
QString join(const QString &separator) const
QTableWidgetItem::setIcon
void setIcon(const QIcon &icon)
QTableWidgetItem::data
virtual QVariant data(int role) const
Practice::SessionManagerBase::allTestEntries
QList< TestEntry * > allTestEntries() const
Get a list of all entries in the test - used by the summary dialog.
Definition: sessionmanagerbase.cpp:167
QTextTable::appendRows
void appendRows(int count)
parleyactions.h
QTextDocumentWriter
QString::number
QString number(int n, int base)
Practice::SessionManagerBase::allEntryCount
int allEntryCount() const
The number of entries available for the practice session.
Definition: sessionmanagerbase.cpp:172
KXmlGuiWindow
QVariant::toInt
int toInt(bool *ok) const
QTextCursor::insertText
void insertText(const QString &text)
QString::isEmpty
bool isEmpty() const
ParleyActions::ExportPracticeResults
Definition: parleyactions.h:37
QTextTableFormat::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag > alignment)
QString
TestEntry::userAnswers
QStringList userAnswers()
Definition: testentry.h:85
Practice::PracticeSummaryComponent::PracticeSummaryComponent
PracticeSummaryComponent(SessionManagerBase *sessionManager, QWidget *parent)
Definition: practicesummarycomponent.cpp:44
QApplication::palette
QPalette palette()
ParleyActions::StartPractice
Definition: parleyactions.h:37
QPalette::foreground
const QBrush & foreground() const
QTextCursor::insertTable
QTextTable * insertTable(int rows, int columns, const QTextTableFormat &format)
QTableWidgetItem::setFlags
void setFlags(QFlags< Qt::ItemFlag > flags)
TestEntry::statisticBadCount
int statisticBadCount()
Definition: testentry.cpp:54
ParleyActions::EnterEditMode
Definition: parleyactions.h:38
Practice::SessionManagerBase::totalTime
int totalTime()
the time in seconds
Definition: sessionmanagerbase.cpp:118
QTextTable::cellAt
QTextTableCell cellAt(int row, int column) const
QTextDocument
TestEntry::languageFrom
int languageFrom() const
Definition: testentry.cpp:109
QTextDocumentWriter::write
bool write(const QTextDocument *document)
QTextTableFormat::setCellPadding
void setCellPadding(qreal padding)
practicesummarycomponent.h
QTextDocument::setHtml
void setHtml(const QString &html)
QTableWidgetItem::setForeground
void setForeground(const QBrush &brush)
TestEntry::correctAtFirstAttempt
bool correctAtFirstAttempt()
Definition: testentry.cpp:124
Practice::SessionManagerBase::statisticTotalCorrectFirstAttempt
int statisticTotalCorrectFirstAttempt()
Definition: sessionmanagerbase.cpp:196
QTextFrameFormat::setBorderStyle
void setBorderStyle(BorderStyle style)
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::SessionManagerBase
Definition: sessionmanagerbase.h:40
Practice::PracticeSummaryComponent::exportResults
void exportResults()
Definition: practicesummarycomponent.cpp:153
QPalette
Practice::SessionManagerBase::statisticTotalWrong
int statisticTotalWrong()
Definition: sessionmanagerbase.cpp:207
TestEntry
Definition: testentry.h:22
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal