/* screenmodel.cpp
 * Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * 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, 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; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
**/
#include "screenmodel.h"
#include <QApplication>
#include <QDebug>
#include "screen-manager.h"
ScreenModel::ScreenModel(QObject *parent)
    : QAbstractListModel(parent)
{
    loadScreens();
    connect(SCREEN_MANAGER, &ScreenManager::virtualDesktopGeometryChanged, this, &ScreenModel::onScreenResized);
    connect(SCREEN_MANAGER, &ScreenManager::screenCountChanged, this, &ScreenModel::onScreenCountChanged);
}

int ScreenModel::rowCount(const QModelIndex &parent) const
{
    if(parent == QModelIndex()){
        return m_screen.size();
    }
    return 0;
}

QVariant ScreenModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()){
        return QVariant();
    }
    int row = index.row();
    if(row < m_screen.size() && role == Qt::UserRole){
        return m_screen[row];
    } else {
        return QVariant();
    }
}

void ScreenModel::onScreenResized(int screen)
{
    if(screen >= 0 && screen < SCREEN_MANAGER->screenCount()){
        m_screen[screen] = SCREEN_MANAGER->allScreens()[screen]->geometry();
    }
    QModelIndex index = createIndex(screen, 0);
    emit dataChanged(index, index);
}

void ScreenModel::onScreenCountChanged(int newCount)
{
    Q_UNUSED(newCount)
    loadScreens();
}

void ScreenModel::loadScreens()
{
    beginResetModel();
    m_screen.clear();
    for(int i = 0; i < SCREEN_MANAGER->screenCount(); i++){
        m_screen.append(SCREEN_MANAGER->allScreens()[i]->geometry());
    }
    endResetModel();
}

