File indexing completed on 2024-06-16 05:24:50
0001 /* 0002 This file is part of the Okteta Kasten module, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2008-2009 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "infoview.hpp" 0010 0011 // tool 0012 #include "infotool.hpp" 0013 #include "statistictablemodel.hpp" 0014 #include "statisticdisplaymodel.hpp" 0015 #include <infoviewsettings.hpp> 0016 // utils 0017 #include <labelledtoolbarwidget.hpp> 0018 // KF 0019 #include <KLocalizedString> 0020 // Qt 0021 #include <QApplication> 0022 #include <QClipboard> 0023 #include <QSortFilterProxyModel> 0024 #include <QToolBar> 0025 #include <QLabel> 0026 #include <QVBoxLayout> 0027 #include <QHeaderView> 0028 #include <QTreeView> 0029 #include <QAction> 0030 #include <QIcon> 0031 #include <QMimeData> 0032 0033 namespace Kasten { 0034 0035 InfoView::InfoView(InfoTool* tool, QWidget* parent) 0036 : QWidget(parent) 0037 , mTool(tool) 0038 { 0039 auto* baseLayout = new QVBoxLayout(this); 0040 baseLayout->setContentsMargins(0, 0, 0, 0); 0041 baseLayout->setSpacing(0); 0042 0043 auto* buildToolBar = new QToolBar(this); 0044 buildToolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle); 0045 0046 auto* label = new QLabel(i18nc("@label size of selected bytes", "Size:"), this); 0047 0048 mSizeLabel = new QLabel(this); 0049 const QString sizeToolTip = 0050 i18nc("@info:tooltip", 0051 "The number of the bytes the statistic was built for."); 0052 label->setToolTip(sizeToolTip); 0053 mSizeLabel->setToolTip(sizeToolTip); 0054 auto* labelledSizeLabel = new LabelledToolBarWidget(label, mSizeLabel, this); 0055 buildToolBar->addWidget(labelledSizeLabel); 0056 connect(mTool->statisticTableModel(), &StatisticTableModel::sizeChanged, 0057 this, &InfoView::setByteArraySize); 0058 0059 auto* stretcher = new QWidget(this); 0060 stretcher->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 0061 buildToolBar->addWidget(stretcher); 0062 0063 mUpdateAction = 0064 buildToolBar->addAction(QIcon::fromTheme(QStringLiteral("run-build")), 0065 i18nc("@action:button build the statistic of the byte frequency", 0066 "&Build"), 0067 mTool, &InfoTool::updateStatistic); 0068 mUpdateAction->setToolTip(i18nc("@info:tooltip", 0069 "Builds the byte frequency statistic for the bytes in the selected range.")); 0070 mUpdateAction->setWhatsThis(xi18nc("@info:whatsthis", 0071 "If you press the <interface>Build</interface> button," 0072 " the byte frequency statistic is built for the bytes in the selected range.")); 0073 mUpdateAction->setEnabled(mTool->isApplyable()); 0074 connect(mTool, &InfoTool::isApplyableChanged, mUpdateAction, &QAction::setEnabled); 0075 0076 baseLayout->addWidget(buildToolBar); 0077 0078 mStatisticTableView = new QTreeView(this); 0079 // TODO: find a signal/event emitted when fixedfont changes 0080 // connect( KGlobalSettings::self(), &KGlobalSettings::kdisplayFontChanged, 0081 // this, &InfoView::setFixedFontByGlobalSettings ); 0082 // connect( KGlobalSettings::self(), &KGlobalSettings::kdisplayFontChanged, 0083 // this, &InfoView::resizeColumnsWidth ); 0084 // connect( KGlobalSettings::self(), &KGlobalSettings::kdisplayStyleChanged, 0085 // this, &InfoView::resizeColumnsWidth ); 0086 mStatisticTableView->setObjectName(QStringLiteral("StatisticTable")); 0087 mStatisticTableView->setRootIsDecorated(false); 0088 mStatisticTableView->setItemsExpandable(false); 0089 mStatisticTableView->setUniformRowHeights(true); 0090 mStatisticTableView->setAllColumnsShowFocus(true); 0091 mStatisticTableView->setSelectionMode(QAbstractItemView::ExtendedSelection); 0092 mStatisticTableView->setDragEnabled(true); 0093 mStatisticTableView->setDragDropMode(QAbstractItemView::DragOnly); 0094 mStatisticTableView->setSortingEnabled(true); 0095 QHeaderView* header = mStatisticTableView->header(); 0096 header->setSectionResizeMode(QHeaderView::Interactive); 0097 header->setStretchLastSection(false); 0098 auto* displayModel = new StatisticDisplayModel(mStatisticTableView, this); 0099 displayModel->setSourceModel(mTool->statisticTableModel()); 0100 auto* proxyModel = new QSortFilterProxyModel(this); 0101 proxyModel->setDynamicSortFilter(true); 0102 proxyModel->setSortRole(StatisticTableModel::SortRole); 0103 proxyModel->setSourceModel(displayModel); 0104 mStatisticTableView->setModel(proxyModel); 0105 mStatisticTableView->sortByColumn(StatisticTableModel::CountId, Qt::DescendingOrder); 0106 connect(mTool->statisticTableModel(), &StatisticTableModel::headerChanged, this, &InfoView::updateHeader); 0107 connect(mStatisticTableView->selectionModel(), &QItemSelectionModel::selectionChanged, 0108 this, &InfoView::onTableSelectionChanged); 0109 0110 baseLayout->addWidget(mStatisticTableView, 10); 0111 0112 // actions 0113 auto* actionsToolBar = new QToolBar(this); 0114 actionsToolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle); 0115 0116 mCopyAction = 0117 actionsToolBar->addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), 0118 i18n("C&opy"), 0119 this, &InfoView::onCopyButtonClicked); 0120 mCopyAction->setToolTip(i18nc("@info:tooltip", 0121 "Copies the selected statistic lines to the clipboard.")); 0122 mCopyAction->setWhatsThis(xi18nc("@info:whatsthis", 0123 "If you press the <interface>Copy</interface> button, all statistic lines you selected " 0124 "in the list are copied to the clipboard.")); 0125 0126 actionsToolBar->addAction(mCopyAction); 0127 0128 auto* actionsStretcher = new QWidget(this); 0129 actionsStretcher->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 0130 actionsToolBar->addWidget(actionsStretcher); 0131 0132 baseLayout->addWidget(actionsToolBar); 0133 0134 // init 0135 setByteArraySize(mTool->size()); 0136 onTableSelectionChanged(); 0137 0138 // if nothing has changed reuse the old values. This means the info view is fully constructed much quicker. 0139 const QList<int> columnsWidth = InfoViewSettings::columnsWidth(); 0140 const QString styleName = QApplication::style()->objectName(); 0141 const QString fontData = mStatisticTableView->font().toString(); 0142 const QString fixedFontData = mTool->statisticTableModel()->fixedFont().toString(); 0143 if ((StatisticTableModel::NoOfIds > columnsWidth.size()) || 0144 (styleName != InfoViewSettings::style()) || 0145 (fontData != InfoViewSettings::font()) || 0146 (fixedFontData != InfoViewSettings::fixedFont())) { 0147 resizeColumnsWidth(); 0148 } else { 0149 for (int i = 0; i < StatisticTableModel::NoOfIds; ++i) { 0150 header->resizeSection(i, columnsWidth.at(i)); 0151 } 0152 } 0153 } 0154 0155 InfoView::~InfoView() 0156 { 0157 QList<int> columnsWidth; 0158 columnsWidth.reserve(StatisticTableModel::NoOfIds); 0159 const QHeaderView* header = mStatisticTableView->header(); 0160 for (int i = 0; i < StatisticTableModel::NoOfIds; ++i) { 0161 columnsWidth.append(header->sectionSize(i)); 0162 } 0163 0164 InfoViewSettings::setColumnsWidth(columnsWidth); 0165 InfoViewSettings::setStyle(QApplication::style()->objectName()); 0166 InfoViewSettings::setFont(mStatisticTableView->font().toString()); 0167 InfoViewSettings::setFixedFont(mTool->statisticTableModel()->fixedFont().toString()); 0168 InfoViewSettings::self()->save(); 0169 } 0170 0171 void InfoView::updateHeader() 0172 { 0173 mStatisticTableView->resizeColumnToContents(StatisticTableModel::ValueId); 0174 mStatisticTableView->header()->headerDataChanged(Qt::Horizontal, 0175 StatisticTableModel::ValueId, StatisticTableModel::ValueId); 0176 } 0177 0178 void InfoView::resizeColumnsWidth() 0179 { 0180 for (int i = 0; i < StatisticTableModel::NoOfIds; ++i) { 0181 mStatisticTableView->resizeColumnToContents(i); 0182 } 0183 } 0184 0185 void InfoView::setByteArraySize(int size) 0186 { 0187 const QString sizeText = (size < 1) ? // -1 is default, 0 should not happen 0188 QStringLiteral("-") : 0189 i18np("1 byte", "%1 bytes", size); 0190 0191 mSizeLabel->setText(sizeText); 0192 } 0193 0194 void InfoView::onCopyButtonClicked() 0195 { 0196 const QItemSelectionModel* selectionModel = mStatisticTableView->selectionModel(); 0197 0198 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); 0199 QMimeData* mimeData = mStatisticTableView->model()->mimeData(selectedIndexes); 0200 0201 QApplication::clipboard()->setMimeData(mimeData); 0202 } 0203 0204 void InfoView::onTableSelectionChanged() 0205 { 0206 const QItemSelectionModel* selectionModel = mStatisticTableView->selectionModel(); 0207 0208 // TODO: selectionModel->selectedIndexes() is a expensive operation, 0209 // but with Qt 4.4.3 hasSelection() has the flaw to return true with a current index 0210 const bool hasSelection = !selectionModel->selectedIndexes().isEmpty(); 0211 mCopyAction->setEnabled(hasSelection); 0212 } 0213 0214 0215 } 0216 0217 #include "moc_infoview.cpp"