Fix the scores window of gtk142

This commit is contained in:
daleclack 2023-01-03 20:33:09 +08:00
parent 1d023d0d97
commit cf1a3729fc
7 changed files with 125 additions and 27 deletions

View File

@ -11,7 +11,8 @@ ScoresWin::ScoresWin(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &
// Create the list store
store = Gtk::ListStore::create(column1);
store->set_sort_column(column1.win_time, Gtk::SortType::DESCENDING);
store->set_sort_column(column1.win_time, Gtk::SortType::ASCENDING);
store->set_sort_func(column1.win_time, sigc::mem_fun(*this, &ScoresWin::sort_func));
// store->set_default_sort_func(sigc::mem_fun(*this, &ScoresWin::sort_func));
tree_view->set_model(store);
tree_view->append_column("name", column1.player_name);
@ -45,17 +46,19 @@ void ScoresWin::update_and_show()
show();
}
int ScoresWin::sort_func(const Gtk::TreeModel::iterator &iter1, const Gtk::TreeModel::iterator &iter2)
int ScoresWin::sort_func(const Gtk::TreeModel::const_iterator &iter1, const Gtk::TreeModel::const_iterator &iter2)
{
// Sort by the game time
auto row1 = *iter1;
auto row2 = *iter2;
if (row1[column1.win_time] < row2[column1.win_time])
{
// g_print("test1\n");
return -1;
}
if (row1[column1.win_time] == row2[column1.win_time])
{
// g_print("test2\n");
return 0;
}
else

View File

@ -31,5 +31,5 @@ class ScoresWin : public Gtk::Window{
Glib::RefPtr<Gtk::ListStore> store;
// Sort function
int sort_func(const Gtk::TreeModel::iterator &iter1, const Gtk::TreeModel::iterator &iter2);
int sort_func(const Gtk::TreeModel::const_iterator &iter1, const Gtk::TreeModel::const_iterator &iter2);
};

View File

@ -1,6 +1,7 @@
#include "MenuWin.hh"
#include "Win1.hh"
#include "Win2.hh"
#include "jsonfile.hh"
WinShown curr_win;
@ -14,7 +15,7 @@ MenuWin::MenuWin()
{
// Initalize window
set_icon_name("org.gtk.daleclack");
set_default_size(300, 200);
set_default_size(1024, 576);
// Add actions
add_action("new_win1", sigc::mem_fun(*this, &MenuWin::new_win1));

View File

@ -16,6 +16,7 @@ private:
Gtk::Box main_box, btn_box;
Gtk::Button btn_main, btn_win1, btn_win2;
Gtk::Label label1;
Gtk::Overlay overlay1;
// Windows
Win1 window1;

View File

@ -0,0 +1,5 @@
#pragma once
#include "../../json_nlohmann/json.hpp"
using json = nlohmann::json;

View File

@ -1,33 +1,116 @@
use std::{fs, io, iter::Map};
/*
Cfgfile example build with Rust,
the original cfgfile is built in Aug 10, 2021,
and this is the rust version
*/
use std::{
fs::File,
io::{self, BufRead, BufReader},
process::exit,
};
fn main() {
let funcs = [about, read_cfg, append_cfg, quit_app];
println!("1.Read a config file;2.Append/Create a config to a config file;3.exit");
// Read user selection
let mut selection = String::new();
loop {
io::stdin()
.read_line(&mut selection)
.expect("Read Line Failed!");
let selection = selection.trim();
if selection == "0" || selection == "1" || selection == "2" || selection == "3" {
let index: usize = selection.parse().unwrap();
funcs[index]();
} else {
println!("Input is vaild!");
}
}
// let v: Vec<&str> = config_str.split('\n').collect();
// println!("The Length:{}", v.len());
// for element in v.iter(){
// println!("{}", element);
// }
// let longterm: Vec<&str> = v[0].split("=").collect();
// let stable: Vec<&str> = v[1].split("=").collect();
// let devel: Vec<&str> = v[2].split("=").collect();
// println!("{}:{}", longterm[0], longterm[1]);
// println!("{}:{}", stable[0], stable[1]);
// println!("{}:{}", devel[0], devel[1]);
}
fn read_cfg_file(filename: String, key: String, value: &mut String) {
// let mut value = String::new();
let key = key.trim();
// Open the file
// If the file not exist, create it
let filename = filename.trim();
let file1 = match File::open(filename) {
Ok(file1) => file1,
Err(_) => File::create(filename).unwrap(),
};
// Create a BufReader and read the file line by line
let reader = BufReader::new(file1);
for (_, line) in reader.lines().enumerate() {
let line = line.unwrap();
// Check the vaild config line
let result = line.find("=");
if result != None {
// split the content line to key and value
let key_value: Vec<&str> = line.split("=").collect();
let key1 = key_value[0].clone();
if key1 == key {
// Push the value
value.push_str(key_value[1]);
return;
}
}
}
}
fn about() {
println!("The Cfgfile test for Rust by daleclack");
}
fn read_cfg() {
println!("Read a config file!");
// Read the filename
println!("Input filename");
let mut filename = String::new();
io::stdin()
.read_line(&mut filename)
.expect("Failed to read the line!");
// Open the file
let filename = filename.trim();
let config_str = match fs::read_to_string(filename) {
Ok(config_str) => config_str,
Err(_) => String::from("-1"),
};
// Read the value
println!("Input Key");
let mut key = String::new();
io::stdin()
.read_line(&mut key)
.expect("Failed to read the line!");
let key1 = key.clone();
//println!("The content of file:{}", config_str);
let v: Vec<&str> = config_str.splitn(3, '\n').collect();
//println!("{}\n{}\n{}\n", v[0], v[1], v[2]);
let longterm: Vec<&str> = v[0].split("=").collect();
let stable: Vec<&str> = v[1].split("=").collect();
let devel: Vec<&str> = v[2].split("=").collect();
println!("{}:{}", longterm[0], longterm[1]);
println!("{}:{}", stable[0], stable[1]);
println!("{}:{}", devel[0], devel[1]);
// Get a line of string
//let config_map : Map<String, String>;
let mut value = String::new();
read_cfg_file(filename, key, &mut value);
println!("{}:{}", key1.trim(), value);
}
fn append_cfg() {
println!("Append a config to cfgfile!");
// Read the filename
println!("Input filename");
let mut filename = String::new();
io::stdin()
.read_line(&mut filename)
.expect("Failed to read the line!");
}
fn quit_app() {
exit(0);
}

View File

@ -1,3 +1,8 @@
This is the config file of Xe Release
See more on github.com/daleclack/Xe-Release
Longterm=5.7
Stable=7.12
Develop=8.0
Release_Path_Unix=/mnt/Dell/Gtk_Dev/Xe-Project
Release_Path_Win32=D:\Gtk_Dev\Xe-Project