mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 23:25:53 +08:00
refs #368, checkbox delegate for editable checkboxes
This commit is contained in:
63
src/blackgui/views/checkboxdelegate.cpp
Normal file
63
src/blackgui/views/checkboxdelegate.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/* Copyright (C) 2013
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "checkboxdelegate.h"
|
||||
#include "../stylesheetutility.h"
|
||||
|
||||
using namespace BlackGui;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Views
|
||||
{
|
||||
|
||||
CCheckBoxDelegate::CCheckBoxDelegate(QObject *parent) : QItemDelegate(parent)
|
||||
{ }
|
||||
|
||||
CCheckBoxDelegate::CCheckBoxDelegate(const QString &iconCheckedUrl, const QString &iconUncheckedUrl, QObject *parent) :
|
||||
QItemDelegate(parent), m_iconCheckedUrl(iconCheckedUrl), m_iconUncheckedUrl(iconUncheckedUrl)
|
||||
{ }
|
||||
|
||||
CCheckBoxDelegate::~CCheckBoxDelegate() { }
|
||||
|
||||
QWidget *CCheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index);
|
||||
Q_UNUSED(option);
|
||||
QCheckBox *cb = new QCheckBox(parent);
|
||||
if (!m_iconCheckedUrl.isEmpty() && !m_iconUncheckedUrl.isEmpty())
|
||||
{
|
||||
QString style = CStyleSheetUtility::styleForIconCheckBox(m_iconCheckedUrl, m_iconUncheckedUrl);
|
||||
cb->setStyleSheet(style);
|
||||
}
|
||||
return cb;
|
||||
}
|
||||
|
||||
void CCheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
bool v = index.model()->data(index, Qt::UserRole).toBool();
|
||||
QCheckBox *cb = qobject_cast<QCheckBox *>(editor);
|
||||
cb->setChecked(v);
|
||||
}
|
||||
|
||||
void CCheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||||
{
|
||||
QCheckBox *cb = qobject_cast<QCheckBox *>(editor);
|
||||
bool v = cb->isChecked();
|
||||
model->setData(index, QVariant(v), Qt::EditRole);
|
||||
}
|
||||
|
||||
void CCheckBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index);
|
||||
editor->setGeometry(option.rect);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
58
src/blackgui/views/checkboxdelegate.h
Normal file
58
src/blackgui/views/checkboxdelegate.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/* Copyright (C) 2013
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKGUI_CCOLUMNCHECKBOXDELEGATE_H
|
||||
#define BLACKGUI_CCOLUMNCHECKBOXDELEGATE_H
|
||||
|
||||
#include <QItemDelegate>
|
||||
#include <QCheckBox>
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Views
|
||||
{
|
||||
|
||||
//! CheckBox for single column
|
||||
class CCheckBoxDelegate : public QItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CCheckBoxDelegate(QObject *parent = nullptr);
|
||||
|
||||
//! Constructor
|
||||
CCheckBoxDelegate(const QString &iconCheckedUrl, const QString &iconUncheckedUrl, QObject *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
~CCheckBoxDelegate();
|
||||
|
||||
//! \copydoc QItemDelegate::createEditor
|
||||
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::setEditorData
|
||||
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::setModelData
|
||||
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::updateEditorGeometry
|
||||
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
|
||||
private:
|
||||
QString m_iconCheckedUrl;
|
||||
QString m_iconUncheckedUrl;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user