2015-04-05 21:57:47 -04:00
|
|
|
#include "ForeignKeyDelegate.hpp"
|
|
|
|
|
2018-11-25 16:55:19 -05:00
|
|
|
#include <QApplication>
|
2015-04-05 21:57:47 -04:00
|
|
|
#include <QComboBox>
|
2018-11-25 16:55:19 -05:00
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QSize>
|
|
|
|
#include <QStyleOptionViewItem>
|
2015-04-05 21:57:47 -04:00
|
|
|
#include "CandidateKeyFilter.hpp"
|
|
|
|
|
2018-11-07 12:49:45 -05:00
|
|
|
ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referenced_model
|
2015-04-05 21:57:47 -04:00
|
|
|
, int referenced_key_column
|
|
|
|
, QObject * parent
|
|
|
|
, int referenced_key_role)
|
|
|
|
: QStyledItemDelegate {parent}
|
2015-05-28 19:22:17 -04:00
|
|
|
, candidate_key_filter_ {new CandidateKeyFilter {referenced_model, referenced_key_column, nullptr, referenced_key_role}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:49:45 -05:00
|
|
|
ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referenced_model
|
2015-05-28 19:22:17 -04:00
|
|
|
, QAbstractItemModel const * referencing_model
|
|
|
|
, int referenced_key_column
|
|
|
|
, int referencing_key_column
|
|
|
|
, QObject * parent
|
|
|
|
, int referenced_key_role
|
|
|
|
, int referencing_key_role)
|
|
|
|
: QStyledItemDelegate {parent}
|
|
|
|
, candidate_key_filter_ {new CandidateKeyFilter {referenced_model, referencing_model, referenced_key_column, referencing_key_column, nullptr, referenced_key_role, referencing_key_role}}
|
2015-04-05 21:57:47 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ForeignKeyDelegate::~ForeignKeyDelegate ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget * ForeignKeyDelegate::createEditor (QWidget * parent
|
|
|
|
, QStyleOptionViewItem const& /* option */
|
|
|
|
, QModelIndex const& index) const
|
|
|
|
{
|
|
|
|
auto editor = new QComboBox {parent};
|
|
|
|
editor->setFrame (false);
|
|
|
|
candidate_key_filter_->set_active_key (index);
|
|
|
|
editor->setModel (candidate_key_filter_.data ());
|
2018-11-11 21:03:26 -05:00
|
|
|
editor->setSizeAdjustPolicy (QComboBox::AdjustToContents);
|
2015-04-05 21:57:47 -04:00
|
|
|
return editor;
|
|
|
|
}
|
2018-11-25 16:55:19 -05:00
|
|
|
|
|
|
|
QSize ForeignKeyDelegate::sizeHint (QStyleOptionViewItem const& option, QModelIndex const& index) const
|
|
|
|
{
|
|
|
|
auto size_hint = QStyledItemDelegate::sizeHint (option, index);
|
|
|
|
QFontMetrics metrics {option.font};
|
|
|
|
QStyleOptionComboBox combo_box_option;
|
|
|
|
combo_box_option.rect = option.rect;
|
|
|
|
combo_box_option.state = option.state | QStyle::State_Enabled;
|
|
|
|
for (auto row = 0; row < candidate_key_filter_->rowCount (); ++row)
|
|
|
|
{
|
|
|
|
size_hint = size_hint.expandedTo (qApp->style ()->sizeFromContents (QStyle::CT_ComboBox
|
|
|
|
, &combo_box_option
|
2019-07-07 20:37:04 -04:00
|
|
|
, metrics.boundingRect (candidate_key_filter_->data (candidate_key_filter_->index (row, 0)).toString ()).size ()));
|
2018-11-25 16:55:19 -05:00
|
|
|
}
|
|
|
|
return size_hint;
|
|
|
|
}
|