1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:07:45 +00:00

DisplaySettings+LibGUI: Move ItemListModel into LibGUI

Without this model, comboboxes are abysmal to work with, so let's not
redefine it every time.
This commit is contained in:
AnotherTest 2020-08-29 08:26:44 +04:30 committed by Andreas Kling
parent 2f9a071072
commit e75247a75b
2 changed files with 17 additions and 13 deletions

View file

@ -25,7 +25,7 @@
*/
#include "DisplaySettings.h"
#include "ItemListModel.h"
#include <LibGUI/ItemListModel.h>
#include <AK/StringBuilder.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/DirIterator.h>
@ -122,7 +122,7 @@ void DisplaySettingsWidget::create_frame()
m_wallpaper_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_wallpaper_combo->set_preferred_size(0, 22);
m_wallpaper_combo->set_only_allow_values_from_model(true);
m_wallpaper_combo->set_model(*ItemListModel<AK::String>::create(m_wallpapers));
m_wallpaper_combo->set_model(*GUI::ItemListModel<AK::String>::create(m_wallpapers));
m_wallpaper_combo->on_change = [this](auto& text, const GUI::ModelIndex& index) {
String path = text;
if (m_monitor_widget->set_wallpaper(path)) {
@ -180,7 +180,7 @@ void DisplaySettingsWidget::create_frame()
m_mode_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_mode_combo->set_preferred_size(0, 22);
m_mode_combo->set_only_allow_values_from_model(true);
m_mode_combo->set_model(*ItemListModel<AK::String>::create(m_modes));
m_mode_combo->set_model(*GUI::ItemListModel<AK::String>::create(m_modes));
m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
this->m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
this->m_monitor_widget->update();
@ -203,7 +203,7 @@ void DisplaySettingsWidget::create_frame()
m_resolution_combo->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_resolution_combo->set_preferred_size(0, 22);
m_resolution_combo->set_only_allow_values_from_model(true);
m_resolution_combo->set_model(*ItemListModel<Gfx::IntSize>::create(m_resolutions));
m_resolution_combo->set_model(*GUI::ItemListModel<Gfx::IntSize>::create(m_resolutions));
m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
this->m_monitor_widget->set_desktop_resolution(m_resolutions.at(index.row()));
this->m_monitor_widget->update();

View file

@ -1,77 +0,0 @@
/*
* Copyright (c) 2019-2020, Jesse Buhgaiar <jooster669@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Vector.h>
#include <LibGUI/Model.h>
template<typename T>
class ItemListModel final : public GUI::Model {
public:
static NonnullRefPtr<ItemListModel> create(Vector<T>& data) { return adopt(*new ItemListModel<T>(data)); }
virtual ~ItemListModel() override { }
virtual int row_count(const GUI::ModelIndex&) const override
{
return m_data.size();
}
virtual int column_count(const GUI::ModelIndex&) const override
{
return 1;
}
virtual String column_name(int) const override
{
return "Data";
}
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
{
if (role == GUI::ModelRole::TextAlignment)
return Gfx::TextAlignment::CenterLeft;
if (role == GUI::ModelRole::Display)
return m_data.at(index.row());
return {};
}
virtual void update() override
{
did_update();
}
private:
explicit ItemListModel(Vector<T>& data)
: m_data(data)
{
}
Vector<T>& m_data;
};