File indexing completed on 2024-05-12 04:37:37

0001 /*
0002     SPDX-FileCopyrightText: 2008 Vladimir Prus <ghost@cs.msu.su>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "treeview.h"
0008 
0009 #include "treemodel.h"
0010 
0011 #include <QApplication>
0012 #include <QDesktopWidget>
0013 #include <QScreen>
0014 
0015 using namespace KDevelop;
0016 
0017 AsyncTreeView::AsyncTreeView(TreeModel& treeModel, QWidget* parent)
0018     : QTreeView(parent)
0019     , m_treeModel(treeModel)
0020     , m_autoResizeColumns(true)
0021 {
0022     connect (this, &AsyncTreeView::expanded,
0023              this, &AsyncTreeView::slotExpanded);
0024     connect (this, &AsyncTreeView::collapsed,
0025              this, &AsyncTreeView::slotCollapsed);
0026     connect (this, &AsyncTreeView::clicked,
0027              this, &AsyncTreeView::slotClicked);
0028     connect(&m_treeModel, &TreeModel::itemChildrenReady, this, &AsyncTreeView::slotExpandedDataReady);
0029 }
0030 
0031 
0032 void AsyncTreeView::slotExpanded(const QModelIndex &index)
0033 {
0034     m_treeModel.expanded(mapViewIndexToTreeModelIndex(index));
0035 }
0036 
0037 void AsyncTreeView::slotCollapsed(const QModelIndex &index)
0038 {
0039     m_treeModel.collapsed(mapViewIndexToTreeModelIndex(index));
0040     resizeColumnsAutomatically();
0041 }
0042 
0043 void AsyncTreeView::slotClicked(const QModelIndex &index)
0044 {
0045     m_treeModel.clicked(mapViewIndexToTreeModelIndex(index));
0046     resizeColumnsAutomatically();
0047 }
0048 
0049 QSize AsyncTreeView::sizeHint() const
0050 {
0051     //Assuming that columns are always resized to fit their contents, return a size that will fit all without a scrollbar
0052     QMargins margins = contentsMargins();
0053     int horizontalSize = margins.left() + margins.right();
0054     for (int i = 0; i < model()->columnCount(); ++i) {
0055         horizontalSize += columnWidth(i);
0056     }
0057     horizontalSize = qMin(horizontalSize, QGuiApplication::primaryScreen()->geometry().width()*3/4);
0058     return QSize(horizontalSize, margins.top() + margins.bottom() + sizeHintForRow(0));
0059 }
0060 
0061 void AsyncTreeView::resizeColumns()
0062 {
0063     for (int i = 0; i < model()->columnCount(); ++i) {
0064         this->resizeColumnToContents(i);
0065     }
0066     this->updateGeometry();
0067 }
0068 
0069 void AsyncTreeView::setAutoResizeColumns(bool on)
0070 {
0071     m_autoResizeColumns = on;
0072     resizeColumnsAutomatically();
0073 }
0074 
0075 TreeModel& AsyncTreeView::treeModel()
0076 {
0077     return m_treeModel;
0078 }
0079 
0080 void AsyncTreeView::slotExpandedDataReady()
0081 {
0082     resizeColumnsAutomatically();
0083 }
0084 
0085 QModelIndex AsyncTreeView::mapViewIndexToTreeModelIndex(const QModelIndex& viewIndex) const
0086 {
0087     return viewIndex;
0088 }
0089 
0090 void AsyncTreeView::resizeColumnsAutomatically()
0091 {
0092     if (m_autoResizeColumns) {
0093         resizeColumns();
0094     }
0095 }
0096 
0097 #include "moc_treeview.cpp"