File indexing completed on 2024-12-22 05:01:02

0001 /*
0002  *   kmail: KDE mail client
0003  *   SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "configuredialoglistview.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QMenu>
0011 
0012 ListView::ListView(QWidget *parent)
0013     : QTreeWidget(parent)
0014 {
0015     setAllColumnsShowFocus(true);
0016     setAlternatingRowColors(true);
0017     setSelectionMode(QAbstractItemView::SingleSelection);
0018     setRootIsDecorated(false);
0019     setContextMenuPolicy(Qt::CustomContextMenu);
0020     connect(this, &ListView::customContextMenuRequested, this, &ListView::slotContextMenu);
0021 }
0022 
0023 void ListView::resizeEvent(QResizeEvent *e)
0024 {
0025     QTreeWidget::resizeEvent(e);
0026     resizeColums();
0027 }
0028 
0029 void ListView::showEvent(QShowEvent *e)
0030 {
0031     QTreeWidget::showEvent(e);
0032     resizeColums();
0033 }
0034 
0035 void ListView::resizeColums()
0036 {
0037     const int c = columnCount();
0038     if (c == 0) {
0039         return;
0040     }
0041 
0042     const int w1 = viewport()->width();
0043     const int w2 = w1 / c;
0044     const int w3 = w1 - (c - 1) * w2;
0045 
0046     for (int i = 0; i < c - 1; ++i) {
0047         setColumnWidth(i, w2);
0048     }
0049     setColumnWidth(c - 1, w3);
0050 }
0051 
0052 void ListView::slotContextMenu(QPoint pos)
0053 {
0054     QMenu menu(this);
0055     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add"), this, &ListView::addHeader);
0056     if (currentItem()) {
0057         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), this, &ListView::removeHeader);
0058     }
0059     menu.exec(viewport()->mapToGlobal(pos));
0060 }
0061 
0062 #include "moc_configuredialoglistview.cpp"