Refinements to the FoxLogWindow widget

This commit is contained in:
Bill Somerville 2018-11-12 04:06:26 +00:00
parent c2ec277cfc
commit 13c023f35d
5 changed files with 49 additions and 102 deletions

View File

@ -0,0 +1,29 @@
#ifndef FONT_OVERRIDE_MODEL_HPP_
#define FONT_OVERRIDE_MODEL_HPP_
#include <QIdentityProxyModel>
#include <QFont>
// fix up font display as header font changes don't currently work
// from views (I think fixed in Qt 5.11.1)
class FontOverrideModel final
: public QIdentityProxyModel
{
public:
FontOverrideModel (QObject * parent = nullptr) : QIdentityProxyModel {parent} {}
void set_font (QFont const& font) {font_ = font;}
QVariant data (QModelIndex const& index, int role) const override
{
if (Qt::FontRole == role) return font_;
return QIdentityProxyModel::data (index, role);
}
QVariant headerData (int section, Qt::Orientation orientation, int role) const override
{
if (Qt::FontRole == role) return font_;
return QIdentityProxyModel::headerData (section, orientation, role);
}
private:
QFont font_;
};
#endif

View File

@ -14,4 +14,5 @@ HEADERS += \
models/Modes.hpp \
models/IARURegions.hpp \
models/FoxLog.hpp \
models/CabrilloLog.hpp
models/CabrilloLog.hpp \
models/FontOverrideModel.hpp

View File

@ -12,64 +12,14 @@
#include "Configuration.hpp"
#include "models/Bands.hpp"
#include "item_delegates/ForeignKeyDelegate.hpp"
#include "item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp"
#include "item_delegates/CallsignDelegate.hpp"
#include "item_delegates/MaidenheadLocatorDelegate.hpp"
#include "widgets/MessageBox.hpp"
#include "qt_helpers.hpp"
#include "ui_FoxLogWindow.h"
namespace
{
class DateTimeAsSecsSinceEpochItemDelegate final
: public QStyledItemDelegate
{
public:
DateTimeAsSecsSinceEpochItemDelegate (QObject * parent = nullptr)
: QStyledItemDelegate {parent}
{
}
static QVariant to_secs_since_epoch (QDateTime const& date_time)
{
return date_time.toMSecsSinceEpoch () / 1000;
}
static QDateTime to_date_time (QModelIndex const& index, int role = Qt::DisplayRole)
{
return to_date_time (index.model ()->data (index, role));
}
static QDateTime to_date_time (QVariant const& value)
{
return QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000);
}
QString displayText (QVariant const& value, QLocale const& locale) const override
{
return locale.toString (to_date_time (value), QLocale::ShortFormat);
}
QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const& /*option*/, QModelIndex const& /*index*/) const override
{
return new QDateTimeEdit {parent};
}
void setEditorData (QWidget * editor, QModelIndex const& index) const override
{
static_cast<QDateTimeEdit *> (editor)->setDateTime (to_date_time (index, Qt::EditRole));
}
void setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const override
{
model->setData (index, to_secs_since_epoch (static_cast<QDateTimeEdit *> (editor)->dateTime ()));
}
void updateEditorGeometry (QWidget * editor, QStyleOptionViewItem const& option, QModelIndex const& /*index*/) const override
{
editor->setGeometry (option.rect);
}
};
}
FoxLogWindow::FoxLogWindow (QSettings * settings, Configuration const * configuration
, QAbstractItemModel * fox_log_model, QWidget * parent)
: QWidget {parent}
@ -84,32 +34,30 @@ FoxLogWindow::FoxLogWindow (QSettings * settings, Configuration const * configur
change_font (configuration_->decoded_text_font ());
ui_->log_table_view->setModel (&fox_log_model_);
ui_->log_table_view->setColumnHidden (0, true);
ui_->log_table_view->setItemDelegateForColumn (1, new DateTimeAsSecsSinceEpochItemDelegate {this});
ui_->log_table_view->setItemDelegateForColumn (6, new ForeignKeyDelegate {configuration_->bands (), &fox_log_model_, 0, 6, this});
ui_->log_table_view->setItemDelegateForColumn (1, new ForeignKeyDelegate {configuration_->bands (), &fox_log_model_, 0, 6, this});
ui_->log_table_view->setItemDelegateForColumn (2, new DateTimeAsSecsSinceEpochDelegate {this});
ui_->log_table_view->setItemDelegateForColumn (3, new CallsignDelegate {this});
ui_->log_table_view->setItemDelegateForColumn (4, new MaidenheadLocatorDelegate {this});
ui_->log_table_view->setSelectionMode (QTableView::SingleSelection);
ui_->log_table_view->resizeColumnsToContents ();
auto horizontal_header = ui_->log_table_view->horizontalHeader ();
horizontal_header->setStretchLastSection (true);
horizontal_header->setSectionResizeMode (QHeaderView::ResizeToContents);
horizontal_header->setSectionsMovable (true);
horizontal_header->moveSection (6, 1); // move band to first column
ui_->rate_label->setNum (0);
ui_->queued_label->setNum (0);
ui_->callers_label->setNum (0);
connect (&fox_log_model_, &QAbstractItemModel::rowsInserted, [this] (QModelIndex const& parent, int first, int /*last*/) {
ui_->log_table_view->scrollTo (fox_log_model_.index (first, 0, parent));
ui_->log_table_view->resizeColumnsToContents ();
// ui_->log_table_view->scrollToBottom ();
ui_->log_table_view->scrollToBottom ();
// ensure view scrolls to latest new row
connect (&fox_log_model_, &QAbstractItemModel::rowsInserted, [this] (QModelIndex const& /*parent*/, int /*first*/, int /*last*/) {
ui_->log_table_view->scrollToBottom ();
});
}
FoxLogWindow::~FoxLogWindow ()
{
if (isVisible ())
{
write_settings ();
}
}
void FoxLogWindow::closeEvent (QCloseEvent * e)
{
write_settings ();
QWidget::closeEvent (e);
}
void FoxLogWindow::read_settings ()

View File

@ -4,6 +4,7 @@
#include <QWidget>
#include <QScopedPointer>
#include <QIdentityProxyModel>
#include "models/FontOverrideModel.hpp"
class QSettings;
class Configuration;
@ -15,28 +16,6 @@ namespace Ui
class FoxLogWindow;
}
// fix up font display as header font changes don't currently work
// from views (I think fixed in Qt 5.11.1)
class FontOverrideModel final
: public QIdentityProxyModel
{
public:
FontOverrideModel (QObject * parent = nullptr) : QIdentityProxyModel {parent} {}
void set_font (QFont const& font) {font_ = font;}
QVariant data (QModelIndex const& index, int role) const override
{
if (Qt::FontRole == role) return font_;
return QIdentityProxyModel::data (index, role);
}
QVariant headerData (int section, Qt::Orientation orientation, int role) const override
{
if (Qt::FontRole == role) return font_;
return QIdentityProxyModel::headerData (section, orientation, role);
}
private:
QFont font_;
};
class FoxLogWindow final
: public QWidget
{
@ -51,8 +30,6 @@ public:
void rate (int);
private:
void closeEvent (QCloseEvent *) override;
void read_settings ();
void write_settings () const;

View File

@ -2,14 +2,6 @@
<ui version="4.0">
<class>FoxLogWindow</class>
<widget class="QWidget" name="FoxLogWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Fox Log</string>
</property>