1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* ComputerManagementView.cpp - provides a view for a network object tree
*
* Copyright (c) 2017-2019 Tobias Junghans <tobydox@veyon.io>
*
* This file is part of Veyon - http://veyon.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <QFileDialog>
#include <QKeyEvent>
#include <QMessageBox>
#include "ComputerManagementView.h"
#include "ComputerManager.h"
#include "NetworkObjectModel.h"
#include "RecursiveFilterProxyModel.h"
#include "RoomSelectionDialog.h"
#include "VeyonConfiguration.h"
#include "ui_ComputerManagementView.h"
ComputerManagementView::ComputerManagementView( ComputerManager& computerManager, QWidget *parent ) :
QWidget(parent),
ui(new Ui::ComputerManagementView),
m_computerManager( computerManager ),
m_filterProxyModel( new RecursiveFilterProxyModel( this ) )
{
m_filterProxyModel->setSourceModel( computerManager.computerTreeModel() );
m_filterProxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
m_filterProxyModel->setFilterKeyColumn( -1 ); // filter all columns instead of first one only
ui->setupUi(this);
// capture keyboard events for tree view
ui->treeView->installEventFilter( this );
// set computer tree model as data model
ui->treeView->setModel( m_filterProxyModel );
// set default sort order
ui->treeView->sortByColumn( 0, Qt::AscendingOrder );
ui->addRoomButton->setVisible( VeyonCore::config().onlyCurrentRoomVisible() &&
VeyonCore::config().manualRoomAdditionAllowed() );
ui->filterLineEdit->setHidden( VeyonCore::config().computerFilterHidden() );
connect( ui->filterLineEdit, &QLineEdit::textChanged,
this, &ComputerManagementView::updateFilter );
}
ComputerManagementView::~ComputerManagementView()
{
delete ui;
}
bool ComputerManagementView::eventFilter( QObject *watched, QEvent *event )
{
if( watched == ui->treeView &&
event->type() == QEvent::KeyPress &&
static_cast<QKeyEvent*>(event)->key() == Qt::Key_Delete )
{
removeRoom();
return true;
}
return QWidget::eventFilter( watched, event );
}
void ComputerManagementView::addRoom()
{
RoomSelectionDialog dialog( m_computerManager.networkObjectModel(), this );
if( dialog.exec() && dialog.selectedRoom().isEmpty() == false )
{
m_computerManager.addRoom( dialog.selectedRoom() );
}
}
void ComputerManagementView::removeRoom()
{
auto model = m_computerManager.computerTreeModel();
const auto index = ui->treeView->selectionModel()->currentIndex();
#if QT_VERSION < 0x050600
#warning Building legacy compat code for unsupported version of Qt
if( index.isValid() &&
static_cast<NetworkObject::Type>( model->data( index, NetworkObjectModel::TypeRole ).toInt() ) == NetworkObject::Group )
#else
if( index.isValid() &&
model->data( index, NetworkObjectModel::TypeRole ).value<NetworkObject::Type>() == NetworkObject::Group )
#endif
{
m_computerManager.removeRoom( model->data( index, NetworkObjectModel::NameRole ).toString() );
}
}
void ComputerManagementView::saveList()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Select output filename" ),
QDir::homePath(), tr( "CSV files (*.csv)" ) );
if( fileName.isEmpty() == false )
{
if( m_computerManager.saveComputerAndUsersList( fileName ) == false )
{
QMessageBox::critical( this, tr( "File error"),
tr( "Could not write the computer and users list to %1! "
"Please check the file access permissions." ).arg( fileName ) );
}
}
}
void ComputerManagementView::updateFilter()
{
const auto filter = ui->filterLineEdit->text();
auto model = ui->treeView->model();
if( filter.isEmpty() )
{
m_filterProxyModel->setFilterWildcard( filter );
for( int i = 0; i < model->rowCount(); ++i )
{
const auto index = model->index( i, 0 );
ui->treeView->setExpanded( index, m_expandedGroups.contains( index ) );
}
m_previousFilter.clear();
}
else
{
if( m_previousFilter.isEmpty() )
{
m_expandedGroups.clear();
for( int i = 0; i < model->rowCount(); ++i )
{
const auto index = model->index( i, 0 );
if( ui->treeView->isExpanded( index ) )
{
m_expandedGroups.append( index );
}
}
}
m_previousFilter = filter;
m_filterProxyModel->setFilterWildcard( filter );
ui->treeView->expandAll();
}
}
|