-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormEdit.cs
More file actions
506 lines (423 loc) · 17.7 KB
/
FormEdit.cs
File metadata and controls
506 lines (423 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace programm
{
public partial class FormEdit : Form
{
private int countShown;
private int start;
private int numStart;
private int realShown;
Dictionary<int, int> num_table_list;
private int words_count;
private string fileName;
static EditActions action;
List<Pair<string, List<Color>>> all_lines;
List<string> all_words;
List<string> all_tags;
public static List<TableLayoutPanel> all_tables;
bool countOK = true;
bool pagesOK = true;
bool success;
private void initialize_vars()
{
countShown = 20;
start = 0;
numStart = 0;
realShown = 20;
success = false;
num_table_list = new Dictionary<int, int>();
words_count = 0;
fileName = "";
action = new EditActions();
all_lines = new List<Pair<string, List<Color>>>();
all_words = new List<string>();
all_tags = new List<string>();
all_tables = new List<TableLayoutPanel>();
}
public FormEdit(string file_name)
{
InitializeComponent();
initialize_vars();
this.WindowState = FormWindowState.Maximized;
fileName = file_name;
action.read_specification();
download_content(file_name);
setPossiblePages();
create_all_tables();
show_file_content();
}
public static Label create_basic_label(string text)
{
Label label = new Label();
label.Text = text;
label.Dock = DockStyle.Fill;
return label;
}
public static Label create_label(string text)
{
Label label = create_basic_label(text);
label.TextAlign = ContentAlignment.MiddleCenter;
return label;
}
private List<Color> set_color_toList(string tags)
{
List<Color> res = new List<Color>();
for (int i = 0; i < tags.Length * 2 + 1; ++i)
res.Add(Color.Black);
return res;
}
private bool isWord(string word)
{
return word != "" && Char.IsLetter(word[0]);
}
private void download_content(string file_name)
{
StreamReader str_TrTag = new StreamReader(file_name, System.Text.Encoding.UTF8);
string line;
int real_num = 0;
int list_num = 0;
while ((line = str_TrTag.ReadLine()) != null)
{
try
{
string[] words = line.Split('\t');
if (words.Count() > 0)
{
if (Char.IsLetter(words[0][0]))
{
words_count++;
if (words[1].Trim() == "-")
{
words[1] = "N";
}
}
all_words.Add(words[0].Trim());
List<Color> colours = set_color_toList(words[1].Trim());
all_lines.Add(new Pair<string, List<Color>>(words[0].Trim() + "\t" + words[1].Trim() + "\t" + words[2].Trim(), colours));
all_tags.Add(words[1].Trim());
if (isWord(words[0].Trim()))
{
num_table_list[real_num] = list_num;
real_num++;
}
list_num++;
}
}
catch
{
MessageBox.Show("Проверьте, наличие русской буквы \"С\" в тегах, пробела вместо табуляции и наличия лишних строк в конце файла.", "Что-то не так с файлом", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
str_TrTag.Close();
return;
}
}
str_TrTag.Close();
success = true;
}
private void create_all_tables()
{
int cur_num = 1;
int height_for_one = 30;
Button_Creates btnCreator = new Button_Creates();
for (int num = num_table_list[numStart]; num < all_lines.Count(); ++num)
{
if (isWord(all_words[num]))
{
var bb = all_tags[num][0];
int height = 0;
try
{
height = Math.Max(height_for_one * action.cat_tagsCount[bb], height_for_one * 2);
}
catch
{
MessageBox.Show("\"" + all_tags[num] + "\" не может быть тегом\n\n" + "Не расстраивайтесь, Вы можете сохранить внесенные изменения перед закрытием программы;)", "Ошибка с тегом", MessageBoxButtons.OK, MessageBoxIcon.Error);
SaveAsToolStripMenuItem_Click(null, null);
success = false;
Close();
return;
}
ExternalTable ex_table = new ExternalTable(50 + height); //создаем таблицу
Label number = create_label((numStart + cur_num).ToString());
ex_table.table.Controls.Add(number);
ex_table.set_text_to_table(all_lines[num], action, cur_num, btnCreator, this);
//ex_table.table.Controls.Add(Button_Creates.ButtonEdit(num));
ex_table.Add_sentence(all_words, num);
all_tables.Add(ex_table.table);
cur_num++;
if (cur_num > countShown)
{
break;
}
}
}
}
//отображение таблицы
private void show_file_content()
{
ScrollUp(MainPanel);
int y = 90;
int interval_y = 0;
for (int num = 0; num < all_tables.Count(); ++num)
{
all_tables[num].Location = new Point(70, y);
MainPanel.Controls.Add(all_tables[num]);
y += all_tables[num].Height + interval_y; //обновляем координату y Location
}
realShown = all_tables.Count();
//numStart += realShown;
panel2.Location = new Point(panel2.Location.X, y);
panel2.Visible = true;
}
//-------------------переходы между страницами--------------
private void ScrollUp(Panel p)
{
//pos passed in should be negative
using (Control c = new Control() { Parent = p, Dock = DockStyle.Top })
{
p.ScrollControlIntoView(c);
c.Parent = null;
c.Dispose();
}
}
private void clearControls()
{
int full_cnt = MainPanel.Controls.Count-1;
for (int i = 0; i < realShown; ++i)
{
MainPanel.Controls[full_cnt - i].Dispose();
//MainPanel.Controls[full_cnt - i].Enabled = false;
//MainPanel.Controls.RemoveAt(full_cnt - i);
}
all_tables.Clear();
}
private void PrevLabel_Click(object sender, EventArgs e)
{
pagesOK = false;
comboBoxTo.Text = "";
comboBoxTo2.Text = "";
pagesOK = true;
clearControls();
numStart = Math.Max(0, numStart - countShown);
create_all_tables();
show_file_content();
}
private void NextLabel_Click(object sender, EventArgs e)
{
pagesOK = false;
comboBoxTo.Text = "";
comboBoxTo2.Text = "";
pagesOK = true;
clearControls();
numStart += realShown;
if (numStart >= words_count)
{
numStart -= realShown;
MessageBox.Show("Поздравляю! Вы проверили весь файл:)", "УРА", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
create_all_tables();
show_file_content();
}
private void setPossiblePages()
{
int row_count = words_count / countShown;
for (int i = 0; i < row_count; ++i)
{
string cur_row = (i * countShown + 1).ToString() + "-" + ((i + 1) * countShown).ToString();
comboBoxTo.Items.Add((cur_row));
comboBoxTo2.Items.Add((cur_row));
}
if(row_count * countShown < words_count)
{
string cur_row = (row_count * countShown + 1).ToString() + "-" + words_count.ToString();
comboBoxTo.Items.Add((cur_row));
comboBoxTo2.Items.Add((cur_row));
}
}
private void comboBoxCount_SelectedIndexChanged(object sender, EventArgs e)
{
if (!countOK)
return;
countOK = false;
ComboBox cmb = (ComboBox)sender;
comboBoxTo.Text = "";
comboBoxTo2.Text = "";
clearControls();
int.TryParse(cmb.Text, out countShown);
realShown = countShown;
start = 0;
numStart = 0;
pagesOK = false;
comboBoxTo.Items.Clear();
comboBoxTo2.Items.Clear();
comboBoxCount.Text = cmb.Text;
comboBoxCount2.Text = cmb.Text;
setPossiblePages();
pagesOK = true;
create_all_tables();
show_file_content();
countOK = true;
}
private void comboBoxTo_SelectedIndexChanged(object sender, EventArgs e)
{
if (!pagesOK)
return;
ComboBox cmb = (ComboBox)sender;
if (cmb.Text == "")
return;
pagesOK = false;
comboBoxTo.Text = cmb.Text;
comboBoxTo2.Text = cmb.Text;
pagesOK = true;
clearControls();
int.TryParse(cmb.Text.Split('-')[0], out numStart);
numStart--;
updateStartFrom_0();
create_all_tables();
show_file_content();
}
private void updateStartFrom_0()
{
start = 0;
int countWatched = 0;
while(countWatched < numStart)
{
if (Char.IsLetter(all_words[start][0]))
countWatched++;
start++;
}
}
//--------------------------Работа с исправлением-------------------------
public void EditButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
int num;
int.TryParse(btn.Name.Substring(4), out num);
FormEditTags form_edit_tags = new FormEditTags(all_tables[num-1], ref action);
if (form_edit_tags.ShowDialog() == DialogResult.OK)
{
string lemma = ((Label)all_tables[num - 1].GetControlFromPosition(2, 0)).Text.Replace("\r", "");
TableLayoutPanel tag_table = (TableLayoutPanel)all_tables[num - 1].GetControlFromPosition(3, 0);
string tags = get_tags_by_table(ref tag_table, num-1);
int list_num = num_table_list[numStart + num - 1];
all_lines[list_num].Item2[all_lines[list_num].Item2.Count()-1] = ((Label)all_tables[num - 1].GetControlFromPosition(2, 0)).ForeColor;
var old_tags = all_lines[list_num].Item1.Split('\t');
all_lines[list_num].Item1 = old_tags[0] + "\t" + tags + "\t" + lemma;
comboBoxTo.Text = "";
comboBoxTo2.Text = "";
clearControls();
//numStart = Math.Max(0, numStart - realShown);
create_all_tables();
show_file_content();
MainPanel.ScrollControlIntoView(all_tables[num - 1]);
}
//form_edit_tags.Show();
}
//---------------------------Сохранение-----------------------------
private string get_tags_by_table(ref TableLayoutPanel tag_table, int num)
{
int list_num = num_table_list[numStart + num];
List<Color> cur_colors = all_lines[list_num].Item2;
cur_colors.Clear();
cur_colors.Add(((Label)tag_table.GetControlFromPosition(0, 0)).ForeColor);
cur_colors.Add(((Label)tag_table.GetControlFromPosition(1, 0)).ForeColor);
string category_name = ((Label)tag_table.GetControlFromPosition(1, 0)).Text.Replace("\r", "");
char category_tag = action.propName_propTag[category_name];
string result_tags = category_tag.ToString();
string help_Zerotags = "";
for (int row = 1; row < tag_table.RowCount; ++row)
{
string prop_name = ((Label)tag_table.GetControlFromPosition(0, row)).Text.Replace("\r", "");
cur_colors.Add(((Label)tag_table.GetControlFromPosition(0, row)).ForeColor);
if ((Label)tag_table.GetControlFromPosition(1, row) == null)
{
cur_colors.Add(Color.Black);
help_Zerotags += "-";
continue;
}
string prop_val = ((Label)tag_table.GetControlFromPosition(1, row)).Text.Replace("\r", "");
cur_colors.Add(((Label)tag_table.GetControlFromPosition(1, row)).ForeColor);
if (prop_val == "" || prop_val == " ")
{
help_Zerotags += "-";
}
else
{
result_tags += help_Zerotags + action.propName_propTag[category_tag + "_" + prop_name + "_" + prop_val].ToString();
help_Zerotags = "";
}
}
return result_tags;
}
private void save_data_to_file(string filename)
{
StreamWriter result_file = new StreamWriter(filename);
int wordNum = 0;
for (int i = 0; i < all_lines.Count; ++i)
{
string result_line = all_lines[i].Item1.Replace("\n", "").Replace("\r", "");
if (i != all_lines.Count - 1)
result_file.WriteLine(result_line);
else
result_file.Write(result_line);
}
result_file.Close();
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "changed_" + fileName.Split('\\').Last();
if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
return;
save_data_to_file(saveFileDialog1.FileName);
fileName = saveFileDialog1.FileName;
SuccessInfo();
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
save_data_to_file(fileName);
SuccessInfo();
}
//------------------------Info-------------------------------
private void FileInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Файл содержит " + words_count.ToString() + " слов для редактирования", "Информация о файле", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void SuccessInfo()
{
MessageBox.Show("Изменения успешно сохранены в файл " + fileName.Split('\\').Last(), "Успешное сохранение", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//-----------------Closing---------------------
private void FormEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (!success)
{
this.Dispose();
Close();
return;
}
string title = "Закрыть";
string question = "Вы уверены, что хотите закрыть форму?" + System.Environment.NewLine + "Несохраненные изменения не будут применены.";
if (new ConfirmForm(title, question).ShowDialog() == DialogResult.OK)
{
this.Dispose();
Close();
}
else
e.Cancel = true;
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
}