File indexing completed on 2024-06-23 05:28:20

0001 /*
0002 This file is part of LightDM-KDE.
0003 
0004 Copyright 2012 David Edmundson <kde@davidedmundson.co.uk>
0005 
0006 LightDM-KDE is free software: you can redistribute it and/or modify
0007 it under the terms of the GNU General Public License as published by
0008 the Free Software Foundation, either version 3 of the License, or
0009 (at your option) any later version.
0010 
0011 LightDM-KDE is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with LightDM-KDE.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "screensmodel.h"
0021 
0022 #include <QApplication>
0023 #include <QDesktopWidget>
0024 
0025 ScreensModel::ScreensModel(QObject *parent) :
0026     QAbstractListModel(parent)
0027 {
0028     loadScreens();
0029     QDesktopWidget *dw = QApplication::desktop();
0030     connect(dw, SIGNAL(screenCountChanged(int)), SLOT(onScreenCountChanged(int)));
0031     connect(dw, SIGNAL(resized(int)), SLOT(onScreenResized(int)));
0032 
0033     QHash<int, QByteArray> roles;
0034     roles[Qt::UserRole] = "geometry";
0035     setRoleNames(roles);
0036 }
0037 
0038 int ScreensModel::rowCount(const QModelIndex &parent) const
0039 {
0040     if (parent == QModelIndex()) {
0041         return m_screens.size();
0042     }
0043     return 0;
0044 }
0045 
0046 QVariant ScreensModel::data(const QModelIndex &index, int role) const
0047 {
0048     int row = index.row();
0049 
0050     if(row < 0 || row >= m_screens.size()) {
0051         return QVariant();
0052     }
0053 
0054     if (role == Qt::UserRole) {
0055         return m_screens[row];
0056     }
0057     return QVariant();
0058 }
0059 
0060 void ScreensModel::onScreenResized(int screen)
0061 {
0062     QDesktopWidget *dw = QApplication::desktop();
0063 
0064     if (screen >= 0 && screen < m_screens.size()) {
0065         m_screens[screen] = dw->screenGeometry(screen);
0066     }
0067     QModelIndex index = createIndex(screen,0);
0068     dataChanged(index, index);
0069 }
0070 
0071 void ScreensModel::onScreenCountChanged(int newCount)
0072 {
0073     Q_UNUSED(newCount);
0074     loadScreens();
0075 }
0076 
0077 void ScreensModel::loadScreens()
0078 {
0079     beginResetModel();
0080     m_screens.clear();
0081     QDesktopWidget *dw = QApplication::desktop();
0082     for (int i=0;i<dw->screenCount();i++) {
0083         m_screens.append(dw->screenGeometry(i));
0084     }
0085     endResetModel();
0086 }