1
+ use convert:: { Convert , conf:: Conf } ;
1
2
use fltk:: {
2
3
prelude:: * ,
3
4
frame:: { self , Frame } ,
@@ -39,6 +40,12 @@ fn main_menu(sys_menu: &mut MyMenu, s: &Sender<Message>) {
39
40
// );
40
41
//}
41
42
43
+ pub fn center ( ) -> ( i32 , i32 ) {
44
+ (
45
+ ( app:: screen_size ( ) . 0 / 2.0 ) as i32 ,
46
+ ( app:: screen_size ( ) . 1 / 2.0 ) as i32 ,
47
+ )
48
+ }
42
49
43
50
44
51
pub struct RpdfApp {
@@ -54,7 +61,7 @@ pub struct RpdfApp {
54
61
55
62
impl RpdfApp {
56
63
pub fn new ( ) -> Self {
57
- let app = app:: App :: default ( ) . with_scheme ( Scheme :: Base ) ;
64
+ let app = app:: App :: default ( ) . with_scheme ( Scheme :: Gtk ) ;
58
65
let ( s, r) = app:: channel :: < Message > ( ) ;
59
66
let mut main_win = Window :: default ( )
60
67
. with_size ( W_WIDTH , W_HEIGHT )
@@ -74,8 +81,9 @@ impl RpdfApp {
74
81
let mut b_section = ButtonSection :: new ( & mut app_flex, 50 , 10 , 10 ) ;
75
82
let mut input_button = b_section. create_input_button ( "@fileopen Open image" . to_string ( ) , 140 ) ;
76
83
input_button. emit ( s, Message :: FileOperation ( FileOperations :: Upload ) ) ;
77
- let mut convert_button = b_section. create_button ( "Convert images" . to_string ( ) , 140 ) ;
78
- convert_button. emit ( s, Message :: FileOperation ( FileOperations :: Convert ) ) ;
84
+ let mut convert_button = b_section. create_button ( "@filenew Convert and save images" . to_string ( ) , 200 ) ;
85
+ convert_button. emit ( s, Message :: FileOperation ( FileOperations :: ConvertAndSave ) ) ;
86
+ convert_button. deactivate ( ) ;
79
87
80
88
b_section. end ( ) ;
81
89
@@ -105,20 +113,63 @@ impl RpdfApp {
105
113
}
106
114
}
107
115
108
- fn open_files_dialog ( & mut self ) {
116
+ fn open_files_dialog ( & mut self ) -> Result < bool , String > {
109
117
let mut dialog = NativeFileChooser :: new ( NativeFileChooserType :: BrowseMultiFile ) ;
110
118
dialog. set_filter ( "*.{png,jpg}" ) ;
111
119
dialog. show ( ) ;
112
- for p in dialog. filenames ( ) . iter ( ) {
113
- let path = p. to_string_lossy ( ) . to_string ( ) ;
114
- if ! self . input_button . get_paths ( ) . contains ( & path) {
120
+ let file_names: Vec < String > = dialog. filenames ( ) . iter ( ) . map ( |p| p. to_string_lossy ( ) . to_string ( ) ) . collect ( ) ;
121
+ if file_names. is_empty ( ) {
122
+ dialog:: message_title ( "Choose file" ) ;
123
+ dialog:: alert ( center ( ) . 0 - 200 , center ( ) . 1 - 100 , "Please choose a file!" ) ;
124
+ return Ok ( false ) ;
125
+ }
126
+
127
+ for path in file_names. iter ( ) {
128
+ if ! self . input_button . get_paths ( ) . contains ( & path. clone ( ) ) {
115
129
self . input_button . add_path ( path. clone ( ) ) ;
116
130
self . p_section . begin ( ) ;
117
- self . p_section . add_image ( path, IMAGE_WIDTH , IMAGE_HEIGTH , IMAGE_PAD , IMAGE_MARGIN ) ;
131
+ self . p_section . add_image ( path. to_string ( ) , IMAGE_WIDTH , IMAGE_HEIGTH , IMAGE_PAD , IMAGE_MARGIN ) ;
118
132
self . p_section . end ( ) ;
119
133
self . p_section . redraw ( ) ;
120
134
}
121
135
}
136
+
137
+ Ok ( true )
138
+ }
139
+
140
+ fn convert_and_save ( & mut self ) -> Result < bool , String > {
141
+ let mut dlg = dialog:: FileDialog :: new ( dialog:: FileDialogType :: BrowseSaveFile ) ;
142
+ dlg. set_option ( dialog:: FileDialogOptions :: SaveAsConfirm ) ;
143
+ dlg. show ( ) ;
144
+ let out = dlg. filename ( ) . to_string_lossy ( ) . to_string ( ) ;
145
+ if out. is_empty ( ) {
146
+ dialog:: message_title ( "Choose save file" ) ;
147
+ dialog:: alert ( center ( ) . 0 - 200 , center ( ) . 1 - 100 , "Please specify a file!" ) ;
148
+ return Ok ( false ) ;
149
+ }
150
+
151
+ self . p_section . flex ( ) . deactivate ( ) ;
152
+ self . b_section . flex ( ) . deactivate ( ) ;
153
+ let images = self . input_button . get_paths ( ) . clone ( ) ;
154
+ let config = Conf :: from_images ( images, out) ;
155
+ let cvrt = Convert :: new ( config) ;
156
+ self . clean_preview_section ( ) ?;
157
+ cvrt. save_to_pdf ( ) ?;
158
+
159
+ self . p_section . flex ( ) . activate ( ) ;
160
+ self . b_section . flex ( ) . activate ( ) ;
161
+ self . b_section . flex ( ) . child ( 1 ) . unwrap ( ) . deactivate ( ) ;
162
+ Ok ( true )
163
+ }
164
+
165
+ fn clean_preview_section ( & mut self ) -> Result < bool , String > {
166
+ if ! self . input_button . clean_images ( ) {
167
+ dialog:: alert ( center ( ) . 0 - 200 , center ( ) . 1 - 100 , "No image in preview section!" ) ;
168
+ return Ok ( false ) ;
169
+ }
170
+ self . p_section . flex ( ) . clear ( ) ;
171
+
172
+ Ok ( true )
122
173
}
123
174
124
175
pub fn launch ( & mut self ) {
@@ -141,13 +192,12 @@ impl RpdfApp {
141
192
} ,
142
193
Message :: FileOperation ( fopt) => match fopt {
143
194
FileOperations :: Upload => {
144
- self . open_files_dialog ( ) ;
195
+ if self . open_files_dialog ( ) . unwrap ( ) {
196
+ self . b_section . flex ( ) . child ( 1 ) . unwrap ( ) . activate ( ) ;
197
+ }
145
198
} ,
146
- FileOperations :: Convert => {
147
- todo ! ( "pass image paths to convert lib then create a handler to save on output name" )
148
- } ,
149
- FileOperations :: Save => {
150
- todo ! ( )
199
+ FileOperations :: ConvertAndSave => {
200
+ self . convert_and_save ( ) ;
151
201
} ,
152
202
} ,
153
203
Message :: PdfSize ( ps) => match ps {
@@ -168,13 +218,26 @@ impl RpdfApp {
168
218
}
169
219
} ,
170
220
Message :: About => {
171
- todo ! ( )
221
+ dialog:: message_title ( "About" ) ;
222
+ dialog:: message ( center ( ) . 0 - 200 , center ( ) . 1 - 100 , "About dialog" ) ;
172
223
} ,
173
224
Message :: Help => {
174
- todo ! ( )
225
+ dialog:: message_title ( "Help" ) ;
226
+ dialog:: message ( center ( ) . 0 - 200 , center ( ) . 1 - 100 , "About dialog" ) ;
175
227
} ,
176
228
Message :: Quit => {
177
- self . app . quit ( ) ;
229
+ if self . input_button . get_paths ( ) . is_empty ( ) {
230
+ self . app . quit ( ) ;
231
+ } else {
232
+ match dialog:: choice2_default ( "Would you like to convert and save" , "No" , "Yes" , "Cancel" ) {
233
+ Some ( 1 ) => self . b_section . flex ( ) . child ( 1 ) . unwrap ( ) . do_callback ( ) ,
234
+ Some ( 0 ) => self . app . quit ( ) ,
235
+ Some ( 2 ) => { } ,
236
+ None => { } ,
237
+ _ => { }
238
+ }
239
+
240
+ }
178
241
} ,
179
242
Message :: None => todo ! ( )
180
243
}
0 commit comments