Cleanup Spreadsheet example

Cleanup the Spreadsheet example:
 - use nullptr
 - use for instead foreach

Change-Id: I55deed157403a46d98a6d753ef46e4cbe5730b4f
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Christian Ehrlicher 2018-11-13 20:43:58 +01:00
parent 18b1dc35e9
commit 8b42614c6c
8 changed files with 55 additions and 73 deletions

View File

@ -53,7 +53,8 @@
#include <QApplication>
#include <QLayout>
int main(int argc, char** argv) {
int main(int argc, char **argv)
{
Q_INIT_RESOURCE(spreadsheet);
QApplication app(argc, argv);
SpreadSheet sheet(10, 6);

View File

@ -48,12 +48,11 @@
**
****************************************************************************/
#include "printview.h"
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif
#include <QStyleOptionViewItem>
#include "printview.h"
PrintView::PrintView()
{

View File

@ -48,36 +48,30 @@
**
****************************************************************************/
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
#include <QPrinter>
#include <QPrintDialog>
#endif
#if QT_CONFIG(printpreviewdialog)
#include <QPrintPreviewDialog>
#endif
#endif
#include "spreadsheet.h"
#include "spreadsheetdelegate.h"
#include "spreadsheetitem.h"
#include "printview.h"
SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
: QMainWindow(parent)
{
addToolBar(toolBar = new QToolBar());
formulaInput = new QLineEdit();
#include <QtWidgets>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport>
#endif
SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
: QMainWindow(parent),
toolBar(new QToolBar(this)),
cellLabel(new QLabel(toolBar)),
table(new QTableWidget(rows, cols, this)),
formulaInput(new QLineEdit(this))
{
addToolBar(toolBar);
cellLabel = new QLabel(toolBar);
cellLabel->setMinimumSize(80, 0);
toolBar->addWidget(cellLabel);
toolBar->addWidget(formulaInput);
table = new QTableWidget(rows, cols, this);
table->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
for (int c = 0; c < cols; ++c) {
QString character(QChar('A' + c));
@ -88,7 +82,7 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
table->setItemDelegate(new SpreadSheetDelegate());
createActions();
updateColor(0);
updateColor(nullptr);
setupMenuBar();
setupContents();
setupContextMenu();
@ -103,7 +97,8 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
this, &SpreadSheet::updateLineEdit);
connect(table, &QTableWidget::itemChanged,
this, &SpreadSheet::updateStatus);
connect(formulaInput, &QLineEdit::returnPressed, this, &SpreadSheet::returnPressed);
connect(formulaInput, &QLineEdit::returnPressed,
this, &SpreadSheet::returnPressed);
connect(table, &QTableWidget::itemChanged,
this, &SpreadSheet::updateLineEdit);
@ -245,11 +240,11 @@ void SpreadSheet::selectColor()
if (!col.isValid())
return;
QList<QTableWidgetItem*> selected = table->selectedItems();
if (selected.count() == 0)
const QList<QTableWidgetItem *> selected = table->selectedItems();
if (selected.isEmpty())
return;
foreach (QTableWidgetItem *i, selected) {
for (QTableWidgetItem *i : selected) {
if (i)
i->setBackgroundColor(col);
}
@ -259,8 +254,8 @@ void SpreadSheet::selectColor()
void SpreadSheet::selectFont()
{
QList<QTableWidgetItem*> selected = table->selectedItems();
if (selected.count() == 0)
const QList<QTableWidgetItem *> selected = table->selectedItems();
if (selected.isEmpty())
return;
bool ok = false;
@ -268,7 +263,7 @@ void SpreadSheet::selectFont()
if (!ok)
return;
foreach (QTableWidgetItem *i, selected) {
for (QTableWidgetItem *i : selected) {
if (i)
i->setFont(fnt);
}
@ -397,7 +392,7 @@ void SpreadSheet::actionSum()
int col_last = 0;
int col_cur = 0;
QList<QTableWidgetItem*> selected = table->selectedItems();
const QList<QTableWidgetItem *> selected = table->selectedItems();
if (!selected.isEmpty()) {
QTableWidgetItem *first = selected.first();
@ -408,7 +403,7 @@ void SpreadSheet::actionSum()
col_last = table->column(last);
}
QTableWidgetItem *current = table->currentItem();
const QTableWidgetItem *current = table->currentItem();
if (current) {
row_cur = table->row(current);
@ -435,7 +430,7 @@ void SpreadSheet::actionMath_helper(const QString &title, const QString &op)
QString cell2 = "C2";
QString out = "C3";
QTableWidgetItem *current = table->currentItem();
const QTableWidgetItem *current = table->currentItem();
if (current)
out = encode_pos(table->currentRow(), table->currentColumn());
@ -468,8 +463,9 @@ void SpreadSheet::actionDivide()
void SpreadSheet::clear()
{
foreach (QTableWidgetItem *i, table->selectedItems())
i->setText("");
const QList<QTableWidgetItem *> selectedItems = table->selectedItems();
for (QTableWidgetItem *i : selectedItems)
i->setText(QString());
}
void SpreadSheet::setupContextMenu()

View File

@ -65,10 +65,8 @@ QT_END_NAMESPACE
class SpreadSheet : public QMainWindow
{
Q_OBJECT
public:
SpreadSheet(int rows, int cols, QWidget *parent = 0);
SpreadSheet(int rows, int cols, QWidget *parent = nullptr);
public slots:
void updateStatus(QTableWidgetItem *item);

View File

@ -53,11 +53,12 @@
#include <QtWidgets>
SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent)
: QItemDelegate(parent) {}
: QItemDelegate(parent)
{}
QWidget *SpreadSheetDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const
const QStyleOptionViewItem &,
const QModelIndex &index) const
{
if (index.column() == 1) {
QDateTimeEdit *editor = new QDateTimeEdit(parent);
@ -92,7 +93,7 @@ void SpreadSheetDelegate::commitAndCloseEditor()
}
void SpreadSheetDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if (edit) {
@ -109,7 +110,8 @@ void SpreadSheetDelegate::setEditorData(QWidget *editor,
}
void SpreadSheetDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model, const QModelIndex &index) const
QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit) {

View File

@ -51,8 +51,6 @@
#ifndef SPREADSHEETDELEGATE_H
#define SPREADSHEETDELEGATE_H
#include "spreadsheet.h"
#include <QItemDelegate>
class SpreadSheetDelegate : public QItemDelegate
@ -60,7 +58,7 @@ class SpreadSheetDelegate : public QItemDelegate
Q_OBJECT
public:
SpreadSheetDelegate(QObject *parent = 0);
SpreadSheetDelegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;

View File

@ -50,19 +50,9 @@
#include "spreadsheetitem.h"
SpreadSheetItem::SpreadSheetItem()
: QTableWidgetItem(), isResolving(false)
{
}
SpreadSheetItem::SpreadSheetItem(const QString &text)
: QTableWidgetItem(text), isResolving(false)
{
}
QTableWidgetItem *SpreadSheetItem::clone() const
{
SpreadSheetItem *item = new SpreadSheetItem();
SpreadSheetItem *item = new SpreadSheetItem;
*item = *this;
return item;
}
@ -75,21 +65,20 @@ QVariant SpreadSheetItem::data(int role) const
if (role == Qt::DisplayRole)
return display();
QString t = display().toString();
bool isNumber = false;
int number = t.toInt(&isNumber);
const QString t = display().toString();
if (role == Qt::ForegroundRole) {
if (!isNumber)
return QVariant::fromValue(QColor(Qt::black));
else if (number < 0)
return QVariant::fromValue(QColor(Qt::red));
return QVariant::fromValue(QColor(Qt::blue));
bool isNumber = false;
const int number = t.toInt(&isNumber);
QColor color = Qt::black;
if (isNumber)
color = (number < 0) ? Qt::red : Qt::blue;
return QVariant::fromValue(color);
}
if (role == Qt::TextAlignmentRole)
if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
return (int)(Qt::AlignRight | Qt::AlignVCenter);
if (role == Qt::TextAlignmentRole)
if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
return int(Qt::AlignRight | Qt::AlignVCenter);
return QTableWidgetItem::data(role);
}

View File

@ -58,8 +58,7 @@
class SpreadSheetItem : public QTableWidgetItem
{
public:
SpreadSheetItem();
SpreadSheetItem(const QString &text);
using QTableWidgetItem::QTableWidgetItem;
QTableWidgetItem *clone() const override;
@ -74,10 +73,10 @@ public:
static QVariant computeFormula(const QString &formula,
const QTableWidget *widget,
const QTableWidgetItem *self = 0);
const QTableWidgetItem *self = nullptr);
private:
mutable bool isResolving;
mutable bool isResolving = false;
};
#endif // SPREADSHEETITEM_H