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:
parent
18b1dc35e9
commit
8b42614c6c
@ -53,7 +53,8 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
Q_INIT_RESOURCE(spreadsheet);
|
Q_INIT_RESOURCE(spreadsheet);
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
SpreadSheet sheet(10, 6);
|
SpreadSheet sheet(10, 6);
|
||||||
|
@ -48,12 +48,11 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "printview.h"
|
||||||
|
|
||||||
#ifndef QT_NO_PRINTER
|
#ifndef QT_NO_PRINTER
|
||||||
#include <QPrinter>
|
#include <QPrinter>
|
||||||
#endif
|
#endif
|
||||||
#include <QStyleOptionViewItem>
|
|
||||||
|
|
||||||
#include "printview.h"
|
|
||||||
|
|
||||||
PrintView::PrintView()
|
PrintView::PrintView()
|
||||||
{
|
{
|
||||||
|
@ -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 "spreadsheet.h"
|
||||||
#include "spreadsheetdelegate.h"
|
#include "spreadsheetdelegate.h"
|
||||||
#include "spreadsheetitem.h"
|
#include "spreadsheetitem.h"
|
||||||
#include "printview.h"
|
#include "printview.h"
|
||||||
|
|
||||||
SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
|
#include <QtWidgets>
|
||||||
: QMainWindow(parent)
|
#if defined(QT_PRINTSUPPORT_LIB)
|
||||||
{
|
#include <QtPrintSupport>
|
||||||
addToolBar(toolBar = new QToolBar());
|
#endif
|
||||||
formulaInput = new QLineEdit();
|
|
||||||
|
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);
|
cellLabel->setMinimumSize(80, 0);
|
||||||
|
|
||||||
toolBar->addWidget(cellLabel);
|
toolBar->addWidget(cellLabel);
|
||||||
toolBar->addWidget(formulaInput);
|
toolBar->addWidget(formulaInput);
|
||||||
|
|
||||||
table = new QTableWidget(rows, cols, this);
|
|
||||||
table->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
|
table->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
|
||||||
for (int c = 0; c < cols; ++c) {
|
for (int c = 0; c < cols; ++c) {
|
||||||
QString character(QChar('A' + c));
|
QString character(QChar('A' + c));
|
||||||
@ -88,7 +82,7 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
|
|||||||
table->setItemDelegate(new SpreadSheetDelegate());
|
table->setItemDelegate(new SpreadSheetDelegate());
|
||||||
|
|
||||||
createActions();
|
createActions();
|
||||||
updateColor(0);
|
updateColor(nullptr);
|
||||||
setupMenuBar();
|
setupMenuBar();
|
||||||
setupContents();
|
setupContents();
|
||||||
setupContextMenu();
|
setupContextMenu();
|
||||||
@ -103,7 +97,8 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
|
|||||||
this, &SpreadSheet::updateLineEdit);
|
this, &SpreadSheet::updateLineEdit);
|
||||||
connect(table, &QTableWidget::itemChanged,
|
connect(table, &QTableWidget::itemChanged,
|
||||||
this, &SpreadSheet::updateStatus);
|
this, &SpreadSheet::updateStatus);
|
||||||
connect(formulaInput, &QLineEdit::returnPressed, this, &SpreadSheet::returnPressed);
|
connect(formulaInput, &QLineEdit::returnPressed,
|
||||||
|
this, &SpreadSheet::returnPressed);
|
||||||
connect(table, &QTableWidget::itemChanged,
|
connect(table, &QTableWidget::itemChanged,
|
||||||
this, &SpreadSheet::updateLineEdit);
|
this, &SpreadSheet::updateLineEdit);
|
||||||
|
|
||||||
@ -245,11 +240,11 @@ void SpreadSheet::selectColor()
|
|||||||
if (!col.isValid())
|
if (!col.isValid())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QList<QTableWidgetItem*> selected = table->selectedItems();
|
const QList<QTableWidgetItem *> selected = table->selectedItems();
|
||||||
if (selected.count() == 0)
|
if (selected.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
foreach (QTableWidgetItem *i, selected) {
|
for (QTableWidgetItem *i : selected) {
|
||||||
if (i)
|
if (i)
|
||||||
i->setBackgroundColor(col);
|
i->setBackgroundColor(col);
|
||||||
}
|
}
|
||||||
@ -259,8 +254,8 @@ void SpreadSheet::selectColor()
|
|||||||
|
|
||||||
void SpreadSheet::selectFont()
|
void SpreadSheet::selectFont()
|
||||||
{
|
{
|
||||||
QList<QTableWidgetItem*> selected = table->selectedItems();
|
const QList<QTableWidgetItem *> selected = table->selectedItems();
|
||||||
if (selected.count() == 0)
|
if (selected.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
@ -268,7 +263,7 @@ void SpreadSheet::selectFont()
|
|||||||
|
|
||||||
if (!ok)
|
if (!ok)
|
||||||
return;
|
return;
|
||||||
foreach (QTableWidgetItem *i, selected) {
|
for (QTableWidgetItem *i : selected) {
|
||||||
if (i)
|
if (i)
|
||||||
i->setFont(fnt);
|
i->setFont(fnt);
|
||||||
}
|
}
|
||||||
@ -397,7 +392,7 @@ void SpreadSheet::actionSum()
|
|||||||
int col_last = 0;
|
int col_last = 0;
|
||||||
int col_cur = 0;
|
int col_cur = 0;
|
||||||
|
|
||||||
QList<QTableWidgetItem*> selected = table->selectedItems();
|
const QList<QTableWidgetItem *> selected = table->selectedItems();
|
||||||
|
|
||||||
if (!selected.isEmpty()) {
|
if (!selected.isEmpty()) {
|
||||||
QTableWidgetItem *first = selected.first();
|
QTableWidgetItem *first = selected.first();
|
||||||
@ -408,7 +403,7 @@ void SpreadSheet::actionSum()
|
|||||||
col_last = table->column(last);
|
col_last = table->column(last);
|
||||||
}
|
}
|
||||||
|
|
||||||
QTableWidgetItem *current = table->currentItem();
|
const QTableWidgetItem *current = table->currentItem();
|
||||||
|
|
||||||
if (current) {
|
if (current) {
|
||||||
row_cur = table->row(current);
|
row_cur = table->row(current);
|
||||||
@ -435,7 +430,7 @@ void SpreadSheet::actionMath_helper(const QString &title, const QString &op)
|
|||||||
QString cell2 = "C2";
|
QString cell2 = "C2";
|
||||||
QString out = "C3";
|
QString out = "C3";
|
||||||
|
|
||||||
QTableWidgetItem *current = table->currentItem();
|
const QTableWidgetItem *current = table->currentItem();
|
||||||
if (current)
|
if (current)
|
||||||
out = encode_pos(table->currentRow(), table->currentColumn());
|
out = encode_pos(table->currentRow(), table->currentColumn());
|
||||||
|
|
||||||
@ -468,8 +463,9 @@ void SpreadSheet::actionDivide()
|
|||||||
|
|
||||||
void SpreadSheet::clear()
|
void SpreadSheet::clear()
|
||||||
{
|
{
|
||||||
foreach (QTableWidgetItem *i, table->selectedItems())
|
const QList<QTableWidgetItem *> selectedItems = table->selectedItems();
|
||||||
i->setText("");
|
for (QTableWidgetItem *i : selectedItems)
|
||||||
|
i->setText(QString());
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpreadSheet::setupContextMenu()
|
void SpreadSheet::setupContextMenu()
|
||||||
|
@ -65,10 +65,8 @@ QT_END_NAMESPACE
|
|||||||
class SpreadSheet : public QMainWindow
|
class SpreadSheet : public QMainWindow
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
SpreadSheet(int rows, int cols, QWidget *parent = nullptr);
|
||||||
SpreadSheet(int rows, int cols, QWidget *parent = 0);
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void updateStatus(QTableWidgetItem *item);
|
void updateStatus(QTableWidgetItem *item);
|
||||||
|
@ -53,11 +53,12 @@
|
|||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
|
|
||||||
SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent)
|
SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent)
|
||||||
: QItemDelegate(parent) {}
|
: QItemDelegate(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
QWidget *SpreadSheetDelegate::createEditor(QWidget *parent,
|
QWidget *SpreadSheetDelegate::createEditor(QWidget *parent,
|
||||||
const QStyleOptionViewItem &,
|
const QStyleOptionViewItem &,
|
||||||
const QModelIndex &index) const
|
const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
if (index.column() == 1) {
|
if (index.column() == 1) {
|
||||||
QDateTimeEdit *editor = new QDateTimeEdit(parent);
|
QDateTimeEdit *editor = new QDateTimeEdit(parent);
|
||||||
@ -92,7 +93,7 @@ void SpreadSheetDelegate::commitAndCloseEditor()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SpreadSheetDelegate::setEditorData(QWidget *editor,
|
void SpreadSheetDelegate::setEditorData(QWidget *editor,
|
||||||
const QModelIndex &index) const
|
const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
|
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
|
||||||
if (edit) {
|
if (edit) {
|
||||||
@ -109,7 +110,8 @@ void SpreadSheetDelegate::setEditorData(QWidget *editor,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SpreadSheetDelegate::setModelData(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);
|
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
|
||||||
if (edit) {
|
if (edit) {
|
||||||
|
@ -51,8 +51,6 @@
|
|||||||
#ifndef SPREADSHEETDELEGATE_H
|
#ifndef SPREADSHEETDELEGATE_H
|
||||||
#define SPREADSHEETDELEGATE_H
|
#define SPREADSHEETDELEGATE_H
|
||||||
|
|
||||||
#include "spreadsheet.h"
|
|
||||||
|
|
||||||
#include <QItemDelegate>
|
#include <QItemDelegate>
|
||||||
|
|
||||||
class SpreadSheetDelegate : public QItemDelegate
|
class SpreadSheetDelegate : public QItemDelegate
|
||||||
@ -60,7 +58,7 @@ class SpreadSheetDelegate : public QItemDelegate
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SpreadSheetDelegate(QObject *parent = 0);
|
SpreadSheetDelegate(QObject *parent = nullptr);
|
||||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
|
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
|
||||||
const QModelIndex &index) const override;
|
const QModelIndex &index) const override;
|
||||||
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||||
|
@ -50,19 +50,9 @@
|
|||||||
|
|
||||||
#include "spreadsheetitem.h"
|
#include "spreadsheetitem.h"
|
||||||
|
|
||||||
SpreadSheetItem::SpreadSheetItem()
|
|
||||||
: QTableWidgetItem(), isResolving(false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
SpreadSheetItem::SpreadSheetItem(const QString &text)
|
|
||||||
: QTableWidgetItem(text), isResolving(false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
QTableWidgetItem *SpreadSheetItem::clone() const
|
QTableWidgetItem *SpreadSheetItem::clone() const
|
||||||
{
|
{
|
||||||
SpreadSheetItem *item = new SpreadSheetItem();
|
SpreadSheetItem *item = new SpreadSheetItem;
|
||||||
*item = *this;
|
*item = *this;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
@ -75,21 +65,20 @@ QVariant SpreadSheetItem::data(int role) const
|
|||||||
if (role == Qt::DisplayRole)
|
if (role == Qt::DisplayRole)
|
||||||
return display();
|
return display();
|
||||||
|
|
||||||
QString t = display().toString();
|
const QString t = display().toString();
|
||||||
bool isNumber = false;
|
|
||||||
int number = t.toInt(&isNumber);
|
|
||||||
|
|
||||||
if (role == Qt::ForegroundRole) {
|
if (role == Qt::ForegroundRole) {
|
||||||
if (!isNumber)
|
bool isNumber = false;
|
||||||
return QVariant::fromValue(QColor(Qt::black));
|
const int number = t.toInt(&isNumber);
|
||||||
else if (number < 0)
|
QColor color = Qt::black;
|
||||||
return QVariant::fromValue(QColor(Qt::red));
|
if (isNumber)
|
||||||
return QVariant::fromValue(QColor(Qt::blue));
|
color = (number < 0) ? Qt::red : Qt::blue;
|
||||||
|
return QVariant::fromValue(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (role == Qt::TextAlignmentRole)
|
if (role == Qt::TextAlignmentRole)
|
||||||
if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
|
if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
|
||||||
return (int)(Qt::AlignRight | Qt::AlignVCenter);
|
return int(Qt::AlignRight | Qt::AlignVCenter);
|
||||||
|
|
||||||
return QTableWidgetItem::data(role);
|
return QTableWidgetItem::data(role);
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,7 @@
|
|||||||
class SpreadSheetItem : public QTableWidgetItem
|
class SpreadSheetItem : public QTableWidgetItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SpreadSheetItem();
|
using QTableWidgetItem::QTableWidgetItem;
|
||||||
SpreadSheetItem(const QString &text);
|
|
||||||
|
|
||||||
QTableWidgetItem *clone() const override;
|
QTableWidgetItem *clone() const override;
|
||||||
|
|
||||||
@ -74,10 +73,10 @@ public:
|
|||||||
|
|
||||||
static QVariant computeFormula(const QString &formula,
|
static QVariant computeFormula(const QString &formula,
|
||||||
const QTableWidget *widget,
|
const QTableWidget *widget,
|
||||||
const QTableWidgetItem *self = 0);
|
const QTableWidgetItem *self = nullptr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable bool isResolving;
|
mutable bool isResolving = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SPREADSHEETITEM_H
|
#endif // SPREADSHEETITEM_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user