MailTransport

transportlistview.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  Based on KMail code by:
5  SPDX-FileCopyrightText: 2002 Marc Mutz <[email protected]>
6  SPDX-FileCopyrightText: 2007 Mathias Soeken <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #include "transportlistview.h"
12 #include "transport.h"
13 #include "transportmanager.h"
14 #include "transporttype.h"
15 
16 #include <QHeaderView>
17 #include <QLineEdit>
18 
19 #include "mailtransport_debug.h"
20 #include <KLocalizedString>
21 
22 using namespace MailTransport;
23 
24 TransportListView::TransportListView(QWidget *parent)
25  : QTreeWidget(parent)
26 {
27  setHeaderLabels(QStringList() << i18nc("@title:column email transport name", "Name") << i18nc("@title:column email transport type", "Type"));
28  setRootIsDecorated(false);
29  header()->setSectionsMovable(false);
30  header()->setSectionResizeMode(QHeaderView::ResizeToContents);
31  setAllColumnsShowFocus(true);
32  setAlternatingRowColors(true);
33  setSortingEnabled(true);
34  sortByColumn(0, Qt::AscendingOrder);
35  setSelectionMode(SingleSelection);
36 
37  fillTransportList();
38  connect(TransportManager::self(), &TransportManager::transportsChanged, this, &TransportListView::fillTransportList);
39 }
40 
41 void TransportListView::editItem(QTreeWidgetItem *item, int column)
42 {
43  // TODO: is there a nicer way to make only the 'name' column editable?
44  if (column == 0 && item) {
45  Qt::ItemFlags oldFlags = item->flags();
46  item->setFlags(oldFlags | Qt::ItemIsEditable);
47  QTreeWidget::editItem(item, 0);
48  item->setFlags(oldFlags);
49  const int id = item->data(0, Qt::UserRole).toInt();
51  if (!t) {
52  qCWarning(MAILTRANSPORT_LOG) << "Transport" << id << "not known by manager.";
53  return;
54  }
55  if (TransportManager::self()->defaultTransportId() == t->id()) {
56  item->setText(0, t->name());
57  }
58  }
59 }
60 
61 void TransportListView::commitData(QWidget *editor)
62 {
63  if (selectedItems().isEmpty()) {
64  // transport was deleted by someone else???
65  qCDebug(MAILTRANSPORT_LOG) << "No selected item.";
66  return;
67  }
68  QTreeWidgetItem *item = selectedItems().first();
69  auto edit = dynamic_cast<QLineEdit *>(editor); // krazy:exclude=qclasses
70  Q_ASSERT(edit); // original code had if
71 
72  const int id = item->data(0, Qt::UserRole).toInt();
74  if (!t) {
75  qCWarning(MAILTRANSPORT_LOG) << "Transport" << id << "not known by manager.";
76  return;
77  }
78  qCDebug(MAILTRANSPORT_LOG) << "Renaming transport" << id << "to" << edit->text();
79  t->setName(edit->text());
80  t->forceUniqueName();
81  t->save();
82 }
83 
84 void TransportListView::fillTransportList()
85 {
86  // try to preserve the selection
87  int selected = -1;
88  if (currentItem()) {
89  selected = currentItem()->data(0, Qt::UserRole).toInt();
90  }
91 
92  clear();
93  const auto lstTransports = TransportManager::self()->transports();
94  for (Transport *t : lstTransports) {
95  auto item = new QTreeWidgetItem(this);
96  item->setData(0, Qt::UserRole, t->id());
97  QString name = t->name();
98  if (TransportManager::self()->defaultTransportId() == t->id()) {
99  name += i18nc("@label the default mail transport", " (Default)");
100  QFont font(item->font(0));
101  font.setBold(true);
102  item->setFont(0, font);
103  }
104  item->setText(0, name);
105  item->setText(1, t->transportType().name());
106  if (t->id() == selected) {
107  setCurrentItem(item);
108  }
109  }
110 }
void setFlags(Qt::ItemFlags flags)
void edit(const QModelIndex &index)
UserRole
Transport * transportById(int id, bool def=true) const
Returns the Transport object with the given id.
QList< Transport * > transports() const
Returns a list of all available transports.
AscendingOrder
Q_SCRIPTABLE void transportsChanged()
Emitted when transport settings have changed (by this or any other TransportManager instance).
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void clear()
typedef ItemFlags
QFont font(int column) const const
virtual QVariant data(int column, int role) const const
static TransportManager * self()
Returns the TransportManager instance.
int toInt(bool *ok) const const
void setText(int column, const QString &text)
void setCurrentItem(QTreeWidgetItem *item)
QList< QTreeWidgetItem * > selectedItems() const const
TransportType transportType() const
Returns the type of this transport.
Definition: transport.cpp:304
Represents the settings of a specific mail transport.
Definition: transport.h:32
QString name() const
Returns the i18n'ed name of the transport type.
QString name(StandardShortcut id)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void forceUniqueName()
Makes sure the transport has a unique name.
Definition: transport.cpp:70
void setFont(int column, const QFont &font)
virtual void setData(int column, int role, const QVariant &value)
QTreeWidgetItem * currentItem() const const
Qt::ItemFlags flags() const const
void editItem(QTreeWidgetItem *item, int column)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 03:56:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.