2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "loglistwidget.h"
32 #include <QApplication>
36 #include <QListWidgetItem>
37 #include <QRegularExpression>
39 #include "base/global.h"
40 #include "guiiconprovider.h"
42 LogListWidget::LogListWidget(int maxLines
, const Log::MsgTypes
&types
, QWidget
*parent
)
44 , m_maxLines(maxLines
)
47 // Allow multiple selections
48 setSelectionMode(QAbstractItemView::ExtendedSelection
);
50 QAction
*copyAct
= new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this);
51 QAction
*clearAct
= new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this);
52 connect(copyAct
, &QAction::triggered
, this, &LogListWidget::copySelection
);
53 connect(clearAct
, &QAction::triggered
, this, &LogListWidget::clear
);
56 setContextMenuPolicy(Qt::ActionsContextMenu
);
59 void LogListWidget::showMsgTypes(const Log::MsgTypes
&types
)
62 for (int i
= 0; i
< count(); ++i
) {
63 QListWidgetItem
*tempItem
= item(i
);
64 if (!tempItem
) continue;
66 Log::MsgType itemType
= static_cast<Log::MsgType
>(tempItem
->data(Qt::UserRole
).toInt());
67 setRowHidden(i
, !(m_types
& itemType
));
71 void LogListWidget::keyPressEvent(QKeyEvent
*event
)
73 if (event
->matches(QKeySequence::Copy
))
75 else if (event
->matches(QKeySequence::SelectAll
))
79 void LogListWidget::appendLine(const QString
&line
, const Log::MsgType
&type
)
81 auto *item
= new QListWidgetItem
;
82 // We need to use QLabel here to support rich text
83 QLabel
*lbl
= new QLabel(line
);
84 lbl
->setContentsMargins(4, 2, 4, 2);
85 item
->setSizeHint(lbl
->sizeHint());
86 item
->setData(Qt::UserRole
, type
);
88 setItemWidget(item
, lbl
);
89 setRowHidden(0, !(m_types
& type
));
91 const int nbLines
= count();
93 if (nbLines
> m_maxLines
)
94 delete takeItem(nbLines
- 1);
97 void LogListWidget::copySelection()
99 static const QRegularExpression
htmlTag("<[^>]+>");
101 for (QListWidgetItem
* it
: asConst(selectedItems()))
102 strings
<< static_cast<QLabel
*>(itemWidget(it
))->text().remove(htmlTag
);
104 QApplication::clipboard()->setText(strings
.join('\n'));