forked from tecwindow/WikiSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_article_window.py
More file actions
208 lines (175 loc) · 7.71 KB
/
view_article_window.py
File metadata and controls
208 lines (175 loc) · 7.71 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
#-*- coding: utf-8 -*-
# import project libraries.
import wx
import nlpia2_wikipedia as wikipedia
import pyperclip
import accessible_output2.outputs.auto
import os
import re
from change_theme_dialog import ChangeTheme
from headings_list_dialog import HeadingsListDialog
from settings import Settings
from ChLanguages import *
colourList = ["Aquamarine", "Black", "Blue", "Blue Violet", "Brown", "Cadet Blue", "Coral", "Cornflower Blue", "Cyan", "Dark Grey", "Dark Green", "Brown"]
#View Article window
class ViewArticleWindow(wx.Frame):
def __init__(self, parent, GetValues, handle):
super().__init__(parent, title=_("View article"), size=(560, 600))
self.Center()
self.EnableMaximizeButton(False)
self.GetValues = GetValues
self.Content = ""
self.url = ""
self.title = ""
self.handle = handle
self.o = accessible_output2.outputs.auto.Auto()
self.rand_id = wx.NewIdRef(count=1)
self.CurrentSettings = Settings().ReadSettings()
Panel = wx.Panel(self)
# Create Menus.
menubar = wx.MenuBar()
actions = wx.Menu()
self.CopyArticleItem = actions.Append(-1, _("Copy article\tctrl+shift+c"))
self.CopyArticleItem.Enable(enable=False)
self.CopyArticleLinkItem = actions.Append(-1, _("Copy article link\tctrl+alt+c"))
self.CopyArticleLinkItem.Enable(enable=False)
self.SaveArticleItem = actions.Append(-1, _("Save article\tctrl+s"))
self.SaveArticleItem.Enable(enable=False)
self.GoToHeading = actions.Append(-1, _("Go to a &heading \tCtrl+h"))
self.GoToHeading.Enable(enable=False)
self.ChangeThemeItem = actions.Append(-1, _("Change theme\tctrl+T"))
self.CloseArticleItem = actions.Append(-1, _("Close article window\tctrl+w"))
self.CloseProgramItem = actions.Append(-1, _("Close the program\tctrl+F4"))
menubar.Append(actions, _("Actions"))
self.SetMenuBar(menubar)
self.hotKeys = wx.AcceleratorTable([
(wx.ACCEL_CTRL+wx.ACCEL_SHIFT, ord("C"), self.CopyArticleItem.GetId()),
(wx.ACCEL_CTRL+wx.ACCEL_ALT, ord("C"), self.CopyArticleLinkItem.GetId()),
(wx.ACCEL_CTRL, ord("S"), self.SaveArticleItem.GetId()),
(wx.ACCEL_CTRL, ord("H"), self.GoToHeading.GetId()),
(wx.ACCEL_CTRL, ord("T"), self.ChangeThemeItem.GetId()),
(wx.ACCEL_CTRL, ord("W"), self.CloseArticleItem.GetId()),
(wx.ACCEL_CTRL,wx.WXK_F4, self.CloseProgramItem.GetId()),
(0, wx.WXK_ESCAPE, self.rand_id),
])
Panel.SetAcceleratorTable(self.hotKeys)
# Create RichEdit to View Article Content
self.ArticleTitle = wx.StaticText(Panel, -1, _("Please wait."), pos=(10,10), size=(380,30))
self.ViewArticle = wx.TextCtrl(Panel, -1, pos=(30,40), size=(480,420), style=wx.TE_RICH2+wx.TE_MULTILINE+wx.TE_READONLY)
# Create Buttons
self.CopyArticle = wx.Button(Panel, -1, _("Copy article\t(ctrl+shift+c)"), pos=(10,500), size=(120,30))
self.CopyArticle.Enable(enable=False)
self.SaveArticle = wx.Button(Panel, -1, _("Save article\t(ctrl+s)"), pos=(140,500), size=(120,30))
self.SaveArticle.Enable(enable=False)
self.SaveArticle.SetDefault()
self.CopyArticleLink = wx.Button(Panel, -1, _("Copy article link\t(ctrl+alt+c)"), pos=(270,500), size=(120,30))
self.CopyArticleLink.Enable(enable=False)
self.CloseArticle = wx.Button(Panel, -1, _("Close article\t(ctrl+w)"), pos=(400,500), size=(120,30))
# Show Article window
self.Show()
# events for buttons
self.CopyArticle.Bind(wx.EVT_BUTTON, self.OnCopyArticle)
self.SaveArticle.Bind(wx.EVT_BUTTON, self.OnSaveArticle)
self.CopyArticleLink.Bind(wx.EVT_BUTTON, self.OnCopyArticleLink)
self.CloseArticle.Bind(wx.EVT_BUTTON, self.OnCloseArticle)
self.Bind(wx.EVT_CLOSE, self.OnCloseArticle)
# events for Menus
self.Bind(wx.EVT_MENU, self.OnCopyArticle, self.CopyArticleItem)
self.Bind(wx.EVT_MENU, self.OnCopyArticleLink, self.CopyArticleLinkItem)
self.Bind(wx.EVT_MENU, self.OnSaveArticle, self.SaveArticleItem)
self.Bind(wx.EVT_MENU, self.OnCloseArticle, self.CloseArticleItem)
self.Bind(wx.EVT_MENU, self.OnChangeTheme, self.ChangeThemeItem)
self.Bind(wx.EVT_MENU, self.OnCloseProgram, self.CloseProgramItem)
self.Bind(wx.EVT_MENU, self.OnGoToheading, self.GoToHeading)
self.Bind(wx.EVT_MENU, self.OnEscape, id=self.rand_id)
def OpenThread(self):
try:
self.Content = wikipedia.page(self.GetValues).content
self.o.speak(_("Loading article:"), interrupt=True)
except wikipedia.exceptions.DisambiguationError as e:
mgb = wx.MessageDialog(self, _("""This article is no longer available.
do you want to show similar results for this article?
"""), _("Warning"), style=wx.YES_NO+wx.YES_DEFAULT+wx.ICON_QUESTION+wx.ICON_WARNING)
if mgb.ShowModal() == wx.ID_YES:
self.Destroy()
self.handle.ListResults.SetItems(e.options)
else:
self.Destroy()
self.handle.NumberArticle -=1
return None
except :
wx.MessageBox("There is no internet connection.", "Connection error", style=wx.ICON_ERROR)
self.Destroy()
self.handle.NumberArticle -=1
return None
self.title = wikipedia.page(self.GetValues).title
self.ViewArticle.Value = self.Content
self.SetTitle(_("View {}").format(self.title))
self.ArticleTitle.SetLabel(self.title)
self.GoToHeading.Enable(enable=True)
self.CopyArticleItem.Enable(enable=True)
self.CopyArticleLinkItem.Enable(enable=True)
self.SaveArticleItem.Enable(enable=True)
self.CopyArticle.Enable(enable=True)
self.SaveArticle.Enable(enable=True)
self.CopyArticleLink.Enable(enable=True)
self.url = wikipedia.page(self.GetValues).url
self.o.speak(_("Article loaded"), interrupt=True)
# Copy Article Content
def OnCopyArticle(self, event):
pyperclip.copy(self.Content)
self.o.speak(_("Article copied."), interrupt=False)
# Copy Article Link
def OnCopyArticleLink(self, event):
pyperclip.copy(self.url)
self.o.speak(_("Article link copied."), interrupt=False)
# Save Article On a New File.
def OnSaveArticle(self, event):
SaveFile = wx.FileDialog(self, _("Save {}:").format(self.title), "self.FilePath", F"{self.title}", style=wx.FD_SAVE+wx.FD_OVERWRITE_PROMPT)
SaveFile.Wildcard = "Text files (.txt)|*.txt"
SaveFileResult = SaveFile.ShowModal()
if SaveFileResult == wx.ID_OK:
FilePath = SaveFile.Path
#FileName = SaveFile.Filename
file = open(FilePath, "w", encoding="utf-8")
file.write(self.ViewArticle.Value)
file.close()
else:
return
def OnChangeTheme(self, event):
dialog2 = ChangeTheme()
GetTheme = dialog2.ShowModal()
self.SetBackgroundColour(wx.Colour(colourList[GetTheme]))
self.ViewArticle.SetBackgroundColour(wx.Colour(colourList[GetTheme]))
self.Refresh()
# Close Article Window
def OnCloseArticle(self, event):
self.handle.NumberArticle -= 1
self.Destroy()
# Close Program
def OnCloseProgram(self, event):
state = self.CurrentSettings["CloseMessage"]
if self.handle.NumberArticle == 1:
ArticleCounte = _("There is 1 open article.")
elif self.handle.NumberArticle > 1:
ArticleCounte = _("There are {} open articles.").format(self.handle.NumberArticle)
if (self.handle.NumberArticle >= 1) and (state == "True"):
ConfirmClosProgram = wx.MessageDialog(self, _("""{}
Do you want to close the program anyway?""").format(ArticleCounte), "Confirm", style=wx.YES_NO+wx.YES_DEFAULT+wx.ICON_WARNING+wx.ICON_QUESTION)
if ConfirmClosProgram.ShowModal() == wx.ID_YES:
wx.Exit()
else:
return
else:
wx.Exit()
def OnGoToheading(self, event):
position = HeadingsListDialog(self, self.Content).ShowModal()
self.ViewArticle.SetInsertionPoint(position)
self.ViewArticle.SetFocus()
def OnEscape(self, event):
state = self.CurrentSettings["ActivEscape"]
if state == "True":
self.handle.NumberArticle -= 1
self.Destroy()
else:
pass