File indexing completed on 2024-05-12 17:15:12

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "mainwidget.h"
0025 
0026 #include "argumentsmodel.h"
0027 #include "eavesdroppermodel.h"
0028 #include "messagesortfilter.h"
0029 
0030 MainWidget::MainWidget()
0031 {
0032     m_ui.setupUi(this);
0033 
0034     m_model = new EavesdropperModel(this);
0035     m_sortFilter = new MessageSortFilter(m_model);
0036     m_sortFilter->setSourceModel(m_model);
0037 
0038     connect(m_ui.captureButton, SIGNAL(toggled(bool)), m_model, SLOT(setRecording(bool)));
0039     connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
0040     connect(m_ui.filterText, SIGNAL(textChanged(QString)), m_sortFilter, SLOT(setFilterString(QString)));
0041     connect(m_ui.unansweredCheckbox, SIGNAL(toggled(bool)), m_sortFilter, SLOT(setOnlyUnanswered(bool)));
0042     connect(m_ui.groupCheckbox, SIGNAL(toggled(bool)), this, SLOT(setGrouping(bool)));
0043     connect(m_ui.messageList, SIGNAL(clicked(QModelIndex)), this, SLOT(itemClicked(QModelIndex)));
0044 
0045     m_ui.messageList->setModel(m_sortFilter);
0046     m_ui.messageList->setAlternatingRowColors(true);
0047     m_ui.messageList->setUniformRowHeights(true);
0048 
0049     m_ui.arguments->setModel(createArgumentsModel(nullptr));
0050     m_ui.arguments->resizeColumnToContents(0);
0051 
0052     // TODO: when a new item appears (what to do if it comes from filter changes?), and the list view scroll
0053     //       position is at the bottom, keep it scrolled to the bottom (that is scroll after appending).
0054     //       It looks about like this in Konversation: if (wasAtBottom) m_bar->setValue(m_bar->maximum());
0055 }
0056 
0057 void MainWidget::clear()
0058 {
0059     m_ui.arguments->setModel(createArgumentsModel(nullptr));
0060     m_model->clear();
0061 }
0062 
0063 void MainWidget::setGrouping(bool enable)
0064 {
0065     m_sortFilter->sort(enable ? 0 : -1); // the actual column (if >= 0) is ignored in the proxy model
0066 }
0067 
0068 void MainWidget::itemClicked(const QModelIndex &index)
0069 {
0070     QAbstractItemModel *oldModel = m_ui.arguments->model();
0071     const int row = m_sortFilter->mapToSource(index).row();
0072     m_ui.arguments->setModel(createArgumentsModel(m_model->m_messages[row].message));
0073     m_ui.arguments->expandAll();
0074 
0075     // increase the first column's width if necessary, never shrink it automatically.
0076     QAbstractItemView *aiv = m_ui.arguments; // sizeHintForColumn is only protected in the subclass?!
0077     QHeaderView *headerView = m_ui.arguments->header();
0078     headerView->resizeSection(0, qMax(aiv->sizeHintForColumn(0), headerView->sectionSize(0)));
0079     delete oldModel;
0080 }
0081 
0082 void MainWidget::load(const QString &filePath)
0083 {
0084     m_model->loadFromFile(filePath);
0085 }
0086 
0087 void MainWidget::save(const QString &filePath)
0088 {
0089     m_model->saveToFile(filePath);
0090 }