1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* Copyright (c) 2012-2014 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
/*
* Copyright (c) 2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/QtUI/QtVCardWidget/QtVCardOrganizationField.h>
#include <boost/algorithm/string.hpp>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <Swift/QtUI/QtSwiftUtil.h>
namespace Swift {
QtVCardOrganizationField::QtVCardOrganizationField(QWidget* parent, QGridLayout *layout, bool editable) :
QtVCardGeneralField(parent, layout, editable, layout->rowCount(), tr("Organization"), false, false), organizationLabel(nullptr), organizationLineEdit(nullptr), unitsTreeWidget(nullptr), itemDelegate(nullptr) {
connect(this, SIGNAL(editableChanged(bool)), SLOT(handleEditibleChanged(bool)));
}
QtVCardOrganizationField::~QtVCardOrganizationField() {
disconnect(this, SLOT(handleEditibleChanged(bool)));
}
void QtVCardOrganizationField::setupContentWidgets() {
organizationLabel = new QLabel(this);
organizationLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
organizationLineEdit = new QtResizableLineEdit(this);
QHBoxLayout* organizationLayout = new QHBoxLayout();
organizationLayout->addWidget(organizationLabel);
organizationLayout->addWidget(organizationLineEdit);
getGridLayout()->addLayout(organizationLayout, getGridLayout()->rowCount()-1, 2, 1, 2, Qt::AlignVCenter);
itemDelegate = new QtRemovableItemDelegate(style());
unitsTreeWidget = new QTreeWidget(this);
connect(unitsTreeWidget->model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), SLOT(handleRowsRemoved(QModelIndex,int,int)));
unitsTreeWidget->setColumnCount(2);
unitsTreeWidget->header()->setStretchLastSection(false);
unitsTreeWidget->header()->resizeSection(1, itemDelegate->sizeHint(QStyleOptionViewItem(), QModelIndex()).width());
#if QT_VERSION >= 0x050000
unitsTreeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
#else
unitsTreeWidget->header()->setResizeMode(0, QHeaderView::Stretch);
#endif
unitsTreeWidget->setHeaderHidden(true);
unitsTreeWidget->setRootIsDecorated(false);
unitsTreeWidget->setEditTriggers(QAbstractItemView::DoubleClicked);
unitsTreeWidget->setItemDelegateForColumn(1, itemDelegate);
connect(unitsTreeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)), SLOT(handleItemChanged(QTreeWidgetItem*,int)));
getGridLayout()->addWidget(unitsTreeWidget, getGridLayout()->rowCount()-1, 4, 2, 1);
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
item->setFlags(item->flags() | Qt::ItemIsEditable);
unitsTreeWidget->addTopLevelItem(item);
getTagComboBox()->hide();
organizationLabel->hide();
childWidgets << organizationLabel << organizationLineEdit << unitsTreeWidget;
}
bool QtVCardOrganizationField::isEmpty() const {
return organizationLineEdit->text().isEmpty() && unitsTreeWidget->model()->rowCount() != 0;
}
void QtVCardOrganizationField::setOrganization(const VCard::Organization& organization) {
organizationLineEdit->setText(P2QSTRING(organization.name));
unitsTreeWidget->clear();
for (const auto& unit : organization.units) {
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList(P2QSTRING(unit)) << "");
item->setFlags(item->flags() | Qt::ItemIsEditable);
unitsTreeWidget->addTopLevelItem(item);
}
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
item->setFlags(item->flags() | Qt::ItemIsEditable);
unitsTreeWidget->addTopLevelItem(item);
}
VCard::Organization QtVCardOrganizationField::getOrganization() const {
VCard::Organization organization;
organization.name = Q2PSTRING(organizationLineEdit->text());
for(int i=0; i < unitsTreeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem* row = unitsTreeWidget->topLevelItem(i);
if (!row->text(0).isEmpty()) {
organization.units.push_back(Q2PSTRING(row->text(0)));
}
}
return organization;
}
void QtVCardOrganizationField::handleEditibleChanged(bool isEditable) {
assert(organizationLineEdit);
assert(unitsTreeWidget);
organizationLineEdit->setVisible(isEditable);
organizationLabel->setVisible(!isEditable);
if (!isEditable) {
QString label;
for(int i=0; i < unitsTreeWidget->topLevelItemCount(); ++i) {
QTreeWidgetItem* row = unitsTreeWidget->topLevelItem(i);
if (!row->text(0).isEmpty()) {
label += row->text(0) + ", ";
}
}
label += organizationLineEdit->text();
organizationLabel->setText(label);
}
unitsTreeWidget->setVisible(isEditable);
}
void QtVCardOrganizationField::handleItemChanged(QTreeWidgetItem *, int) {
guaranteeEmptyRow();
}
void QtVCardOrganizationField::handleRowsRemoved(const QModelIndex&, int, int) {
guaranteeEmptyRow();
}
void QtVCardOrganizationField::guaranteeEmptyRow() {
bool hasEmptyRow = false;
QList<QTreeWidgetItem*> rows = unitsTreeWidget->findItems("", Qt::MatchFixedString);
for (auto row : rows) {
if (row->text(0).isEmpty()) {
hasEmptyRow = true;
}
}
if (!hasEmptyRow) {
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList("") << "");
item->setFlags(item->flags() | Qt::ItemIsEditable);
unitsTreeWidget->addTopLevelItem(item);
unitsTreeWidget->setCurrentItem(item);
}
}
}
|