From cf1a3729fc305bbcc51f1fdcd033fff362ed40e2 Mon Sep 17 00:00:00 2001 From: daleclack Date: Tue, 3 Jan 2023 20:33:09 +0800 Subject: [PATCH] Fix the scores window of gtk142 --- Gtkmm4/gtk142_minesweeper3/src/ScoresWin.cc | 7 +- Gtkmm4/gtk142_minesweeper3/src/ScoresWin.hh | 2 +- Gtkmm4/gtk143_menutest2/src/MenuWin.cc | 3 +- Gtkmm4/gtk143_menutest2/src/MenuWin.hh | 1 + Gtkmm4/gtk143_menutest2/src/jsonfile.hh | 5 + Rust_tests/rust_cfgfile/src/main.rs | 129 ++++++++++++++++---- Rust_tests/rust_cfgfile/xe_config | 5 + 7 files changed, 125 insertions(+), 27 deletions(-) create mode 100644 Gtkmm4/gtk143_menutest2/src/jsonfile.hh diff --git a/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.cc b/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.cc index ea2450c..242d520 100644 --- a/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.cc +++ b/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.cc @@ -11,7 +11,8 @@ ScoresWin::ScoresWin(BaseObjectType *cobject, const Glib::RefPtr & // 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 diff --git a/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.hh b/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.hh index f87ec2f..21e1d0f 100644 --- a/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.hh +++ b/Gtkmm4/gtk142_minesweeper3/src/ScoresWin.hh @@ -31,5 +31,5 @@ class ScoresWin : public Gtk::Window{ Glib::RefPtr 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); }; diff --git a/Gtkmm4/gtk143_menutest2/src/MenuWin.cc b/Gtkmm4/gtk143_menutest2/src/MenuWin.cc index 980ecf4..ea62415 100644 --- a/Gtkmm4/gtk143_menutest2/src/MenuWin.cc +++ b/Gtkmm4/gtk143_menutest2/src/MenuWin.cc @@ -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)); diff --git a/Gtkmm4/gtk143_menutest2/src/MenuWin.hh b/Gtkmm4/gtk143_menutest2/src/MenuWin.hh index 7811bc5..f56b761 100644 --- a/Gtkmm4/gtk143_menutest2/src/MenuWin.hh +++ b/Gtkmm4/gtk143_menutest2/src/MenuWin.hh @@ -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; diff --git a/Gtkmm4/gtk143_menutest2/src/jsonfile.hh b/Gtkmm4/gtk143_menutest2/src/jsonfile.hh new file mode 100644 index 0000000..f2fedbd --- /dev/null +++ b/Gtkmm4/gtk143_menutest2/src/jsonfile.hh @@ -0,0 +1,5 @@ +#pragma once + +#include "../../json_nlohmann/json.hpp" + +using json = nlohmann::json; \ No newline at end of file diff --git a/Rust_tests/rust_cfgfile/src/main.rs b/Rust_tests/rust_cfgfile/src/main.rs index 98cc2b5..5fcf838 100644 --- a/Rust_tests/rust_cfgfile/src/main.rs +++ b/Rust_tests/rust_cfgfile/src/main.rs @@ -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; + 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); } diff --git a/Rust_tests/rust_cfgfile/xe_config b/Rust_tests/rust_cfgfile/xe_config index c6f1581..bc70a84 100644 --- a/Rust_tests/rust_cfgfile/xe_config +++ b/Rust_tests/rust_cfgfile/xe_config @@ -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