Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

初步支持句库

  • Loading branch information...
commit 400d05946971717f8a3d682f907f114dacf8255a 1 parent df01766
@muzuiget authored
View
65 strengthword/const.py
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
-common_js = u'''
+wordview_common_js = u'''
(function() {
// 删除「生词本」
@@ -19,7 +19,7 @@
}());
'''
-window_js = u'''
+wordview_js = u'''
(function() {
// 添加查询的单词
@@ -41,7 +41,7 @@
}());
'''
-popup_js = u'''
+popup_wordview_js = u'''
(function() {
// 添加查询的单词以及「详细」链接
@@ -64,12 +64,30 @@
}());
'''
-not_fount_html = u'''
+sentenceview_js = u'''
+(function() {
+
+ // 宽度修复
+ var node = document.getElementById('right');
+ //node.style.width = '100%';
+
+ // 双击选择跳转
+ document.addEventListener('dblclick', function() {
+ var selected = document.getSelection().toString().trim();
+ if (selected == '') {
+ return;
+ }
+ location.href = location.href.replace(/&s=.+$/, '&s=' + selected);
+ })
+
+}());
+'''
+
+wordview_not_found_html = u'''
<!DOCTYPE HTML>
<html>
<head>
- <meta charset="UTF-8">
- <title>not found</title>
+ <meta charset="UTF-8"> <title>not found</title>
<style type="text/css">
body {
font-size: 13px;
@@ -81,7 +99,7 @@
<p>当前词典中暂无与「%(word)s」相符的解释</p>
-<strong>建议您<strong>
+<strong>建议您<strong>
<p>1. 去爱词霸网站<a href="http://bbs.iciba.com/forum.php?mod=forumdisplay&fid=52">发帖求助</a>;</p>
@@ -91,7 +109,7 @@
</html>
'''
-too_long_html = u'''
+popup_wordview_too_long_html = u'''
<!DOCTYPE HTML>
<html>
<head>
@@ -110,3 +128,34 @@
</body>
</html>
'''
+
+sentenceview_not_found_html = u'''
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>not found</title>
+ <style type="text/css">
+ body {
+ font-size: 13px;
+ margin: 16px;
+ }
+ </style>
+</head>
+<body>
+
+<p>很抱歉,暂无与「%(word)s」 相关的例句。</p>
+
+<strong>建议您:<strong>
+
+<p>1. 检查输入的文字是否有误;</p>
+
+<p>2. 去掉可能不必要的字词,如「的」、「什么」等;</p>
+
+<p>3. 搜索<a href="http://www.sogou.com/sogou?query=%(word)s">%(word)s</a>;</p>
+
+<p>4. 您可以试着自己<a href="http://dj.iciba.com/v5/djadd.php">添加例句</a>。</p>
+
+</body>
+</html>
+'''
View
227 strengthword/dictview.py
@@ -11,28 +11,17 @@
import const
from minix import QWidgetMinix
-class Dictview(QFrame, QWidgetMinix):
-
- URL = 'http://dict-client.iciba.com/index.php?c=client&word=%s&dictlist=%s'
- DICTLIST = [1,101,3,2,102,4,5,103]
- WIDTH = 700
- HEIGHT = 540
- ALLOW_LENGTH = 25
-
- detailLinkClicked = Signal(str)
+class View(QFrame, QWidgetMinix):
def __init__(self):
- super(Dictview, self).__init__()
+ QFrame.__init__(self)
self.init_window()
self.init_webview()
self.init_toolbar()
self.init_layout()
- self.init_screen_info()
- self.init_config()
def init_window(self):
self.setWindowTitle('StrengthWord')
- self.resize(Dictview.WIDTH, Dictview.HEIGHT)
self.screen_center()
def init_webview(self):
@@ -40,7 +29,6 @@ def init_webview(self):
self.webpage = self.webview.page()
self.webframe = self.webpage.mainFrame()
- self.webpage.setViewportSize(QSize(Dictview.WIDTH, Dictview.HEIGHT))
self.webpage.setLinkDelegationPolicy(
QWebPage.DelegateAllLinks);
@@ -79,44 +67,69 @@ def init_layout(self):
self.layout.addWidget(self.webview)
self.setLayout(self.layout)
- def init_screen_info(self):
- rect = QDesktopWidget().availableGeometry()
- self.screen_width = rect.width()
- self.screen_height = rect.height()
-
- def init_config(self):
- self.dictlist = Dictview.DICTLIST
- self.window_width = Dictview.WIDTH
- self.window_height = Dictview.HEIGHT
- self.allow_length = Dictview.ALLOW_LENGTH
-
def on_query_lineedit_returnPressed(self):
text = self.query_lineedit.text()
self.query(text)
def on_webview_loadFinished(self):
+ raise NotImplementedError
+
+ def on_webview_linkClicked(self, url):
+ raise NotImplementedError
+
+ def enable_debug(self, value=True):
+ ''' 开启开发者工具 '''
+
+ self.webview.settings().setAttribute(
+ QWebSettings.WebAttribute.DeveloperExtrasEnabled, value)
+
+ def current_word(self):
+ return self.query_lineedit.text()
+
+ def set_html(self, html):
+ self.webview.blockSignals(True)
+ self.webview.setHtml(html)
+ self.webview.blockSignals(False)
+
+ def show(self):
+ QFrame.show(self)
+ self.activateWindow()
+ self.query_lineedit.setFocus()
+
+ def create_url(self, text):
+ raise NotImplementedError
+
+ def query(self, text):
+ text = text.strip()
+ if text == '':
+ return
+
+ self.query_lineedit.setText(text)
+ url = self.create_url(text)
+ self.webview.load(url)
+
+class WordView(View):
+
+ def __init__(self):
+ View.__init__(self)
+ self.dictlist = [1,101,3,2,102,4,5,103]
+
+ def on_webview_loadFinished(self):
if self.webview.url().toString() == 'about:blank':
return
word = self.webview.url().queryItemValue('word')
self.query_lineedit.setText(word)
if self.webview.title() == u'爱词霸在线词典':
- self.run_javascript()
+ js = self.get_run_javascript()
+ self.webframe.evaluateJavaScript(js)
else:
- html = const.not_fount_html % {'word': word}
- self.webview.blockSignals(True)
- self.webview.setHtml(html)
- self.webview.blockSignals(False)
+ html = const.wordview_not_found_html % {'word': word}
+ self.set_html(html)
self.show()
def on_webview_linkClicked(self, url):
- if url.fragment() == 'detail':
- self.hide()
- word = url.queryItemValue('word')
- self.detailLinkClicked.emit(word)
- return
-
if url.host() != 'dict-client.iciba.com':
self.hide()
webbrowser.open(url.toString())
@@ -124,49 +137,49 @@ def on_webview_linkClicked(self, url):
self.webview.load(url)
- def enable_debug(self, value=True):
- self.webview.settings().setAttribute(
- QWebSettings.WebAttribute.DeveloperExtrasEnabled, value)
+ def get_run_javascript(self):
+ word = self.webview.url().queryItemValue('word')
+ js = const.wordview_common_js
+ js += const.wordview_js % dict(word=word)
+ return js
- def setWindowFlags(self, flags):
- if flags == Qt.Popup:
- self.setStyleSheet('QFrame {border: 1px solid #55AAFF;}')
- self.toolbar.hide()
- else:
- self.setStyleSheet('')
- self.toolbar.show()
+ def create_url(self, text):
+ url_tpl = 'http://dict-client.iciba.com/index.php?c=client&word=%s&dictlist=%s'
+ url = url_tpl % (text, ','.join(map(str, self.dictlist)))
+ return QUrl(url)
- QFrame.setWindowFlags(self, flags)
+class PopupWordView(WordView):
- def show(self):
- if self.windowType() == Qt.Popup:
- self.fix_page_content_size()
- if not self.isActiveWindow():
- self.follow_mouse()
- self.inside_screen()
- else:
- self.setStyleSheet('')
+ detailLinkClicked = Signal(str)
- QFrame.show(self)
- self.activateWindow()
+ def __init__(self):
+ WordView.__init__(self)
+ self.dictlist = [1, 101]
+ self.setWindowFlags(Qt.Popup)
+ self.setStyleSheet('QFrame {border: 1px solid #55AAFF;}')
+ self.resize(360, 360)
+ self.webpage.setViewportSize(self.size())
+ self.allow_length = 25
+ self.toolbar.hide()
+
+ def on_webview_linkClicked(self, url):
+ if url.fragment() == 'detail':
+ self.hide()
+ word = url.queryItemValue('word')
+ self.detailLinkClicked.emit(word)
+ return
- if self.query_lineedit.isVisible():
- self.query_lineedit.setFocus()
+ WordView.on_webview_linkClicked(self, url)
- def run_javascript(self):
- # 执行一些修复性 Javascript
+ def get_run_javascript(self):
word = self.webview.url().queryItemValue('word')
- if self.windowType() == Qt.Popup:
- window_type_js = const.popup_js % {'word': word}
- else:
- window_type_js = const.window_js % {'word': word}
+ js = const.wordview_common_js
+ js += const.popup_wordview_js % dict(word=word)
+ return js
- self.webframe.addToJavaScriptWindowObject('python', QObject())
- self.webframe.evaluateJavaScript(const.common_js)
- self.webframe.evaluateJavaScript(window_type_js)
+ def auto_resize(self):
+ ''' 用 Javascript 获得页面有效高度 '''
- def fix_page_content_size(self):
- # 用 Javascript 获得页面有效高度
get_new_height = lambda: int(self.webframe.evaluateJavaScript(
'document.documentElement.scrollHeight')) + 2 # 2 是边框
@@ -179,9 +192,12 @@ def fix_page_content_size(self):
if height1 != height2:
self.resize(self.size().width(), height2)
- def create_url(self, text):
- url = Dictview.URL % (text, ','.join(map(str, self.dictlist)))
- return QUrl(url)
+ def show(self):
+ self.auto_resize()
+ if not self.isActiveWindow():
+ self.follow_mouse()
+ self.inside_screen()
+ WordView.show(self)
def query(self, text):
text = text.strip()
@@ -192,27 +208,57 @@ def query(self, text):
# 选中大量文本,不取词
if exceed_count > 0:
- html = const.too_long_html % {'exceed_count': exceed_count}
- self.webview.blockSignals(True)
- self.webview.setHtml(html)
- self.webview.blockSignals(False)
+ html = const.popup_wordview_too_long_html % \
+ {'exceed_count': exceed_count}
+ self.set_html(html)
self.show()
return
+ self.query_lineedit.setText(text)
url = self.create_url(text)
self.webview.load(url)
-def test_window():
+class SentenceView(View):
+
+ def __init__(self):
+ View.__init__(self)
+
+ def on_webview_loadFinished(self):
+ if self.webview.url().toString() == 'about:blank':
+ return
+
+ word = self.webview.url().queryItemValue('s')
+ self.query_lineedit.setText(word)
+ if self.webview.title() == '2012 free interface':
+ js = self.get_run_javascript()
+ self.webframe.evaluateJavaScript(js)
+ else:
+ html = const.sentenceview_not_found_html % {'word': word}
+ self.set_html(html)
+
+ self.show()
+
+ def get_run_javascript(self):
+ return const.sentenceview_js
+
+ def on_webview_linkClicked(self, url):
+ pass
+
+ def create_url(self, text):
+ url = 'http://interface2010.client.iciba.com/?c=dj2012&s=%s' % text
+ return QUrl(url)
+
+def test_wordview():
app = QApplication(sys.argv)
- dictview = Dictview()
- dictview.enable_debug()
- dictview.query('test')
- dictview.show()
+ wordview = WordView()
+ wordview.enable_debug()
+ wordview.query('test')
+ wordview.show()
app.exec_()
-def test_popup():
+def test_popup_wordview():
app = QApplication(sys.argv)
- dictview = Dictview()
+ dictview = PopupWordView()
dictview.setWindowFlags(Qt.Popup)
dictview.dictlist = [1, 101]
dictview.resize(360, 360)
@@ -222,9 +268,18 @@ def test_popup():
exit_button.show()
app.exec_()
+def test_sentenceview():
+ app = QApplication(sys.argv)
+ sentenceview = SentenceView()
+ sentenceview.enable_debug()
+ sentenceview.query('test')
+ sentenceview.show()
+ app.exec_()
+
def main():
- test_window()
- #test_popup()
+ test_wordview()
+ #test_popup_wordview()
+ #test_sentenceview()
if __name__ == '__main__':
main()
View
6 strengthword/minix.py
@@ -26,11 +26,13 @@ def inside_screen(self):
width, height = self.size().toTuple()
# 避免超出屏幕边缘
- right_outside = x + width - self.screen_width
+ rect = QDesktopWidget().availableGeometry()
+
+ right_outside = x + width - rect.width()
if right_outside > 0:
x -= right_outside
- bottom_outside = y + height - self.screen_height
+ bottom_outside = y + height - rect.height()
if bottom_outside > 0:
y -= bottom_outside
View
76 strengthword/strengthword.py
@@ -6,25 +6,43 @@
from PySide.QtGui import *
from PySide.QtWebKit import *
-from dictview import Dictview
+from minix import QWidgetMinix
+from dictview import WordView, PopupWordView, SentenceView
-class StrengthWord(QWidget):
+class StrengthWord(QWidget, QWidgetMinix):
+
+ WIDTH = 700
+ HEIGHT = 540
def __init__(self):
QWidget.__init__(self)
+ self.init_window()
self.init_dictviews()
+ self.init_tabwidget()
self.init_clipboard()
self.init_tray()
+ self.init_layout()
QApplication.setQuitOnLastWindowClosed(False)
+ def init_window(self):
+ self.setWindowTitle('StrengthWord')
+ self.resize(StrengthWord.WIDTH, StrengthWord.HEIGHT)
+ self.screen_center()
+
def init_dictviews(self):
- self.window_dictview = Dictview()
- self.popup_dictview = Dictview()
- self.popup_dictview.setWindowFlags(Qt.Popup)
- self.popup_dictview.dictlist = [1, 101]
- self.popup_dictview.resize(360, 360)
- self.popup_dictview.detailLinkClicked.connect(
- self.window_dictview.query)
+ self.wordview = WordView()
+ self.popup_wordview = PopupWordView()
+ self.popup_wordview.detailLinkClicked.connect(
+ self.wordview.query)
+ self.sentenceview = SentenceView()
+ self.sentenceview.enable_debug()
+
+ def init_tabwidget(self):
+ self.tabwidget = QTabWidget()
+ self.tabwidget.addTab(self.wordview, u'词典')
+ self.tabwidget.addTab(self.sentenceview, u'句库')
+
+ self.tabwidget.currentChanged.connect(self.on_tabwidget_currentChanged)
def init_clipboard(self):
self.clipboard = QApplication.clipboard()
@@ -54,15 +72,21 @@ def init_tray(self):
self.trayicon.setContextMenu(self.trayicon_menu)
self.trayicon.activated.connect(self.on_trayicon_activated)
+ def init_layout(self):
+ self.layout = QVBoxLayout()
+ self.layout.setContentsMargins(0, 0, 0, 0)
+ self.layout.setSpacing(0)
+
+ self.layout.addWidget(self.tabwidget)
+ self.setLayout(self.layout)
+
def show(self):
self.trayicon.show()
- self.window_dictview.show()
+ QWidget.show(self)
+ self.wordview.query_lineedit.setFocus()
def on_show_action_triggered(self):
- if self.show_action.isChecked():
- self.window_dictview.show()
- else:
- self.window_dictview.hide()
+ self.setVisible(self.show_action.isChecked())
def on_scan_action_triggered(self):
if self.scan_action.isChecked():
@@ -76,19 +100,27 @@ def on_trayicon_activated(self, reason):
if reason != QSystemTrayIcon.Trigger:
return
- visible = self.window_dictview.isVisible()
- if visible:
- self.window_dictview.hide()
- else:
- self.window_dictview.show()
+ visible = self.isVisible()
+ self.setVisible(not visible)
self.show_action.setChecked(not visible)
def on_clipboard_selectionChanged(self):
text = self.clipboard.text(QClipboard.Selection)
- self.popup_dictview.query(text)
+ self.popup_wordview.query(text)
+
+ def on_tabwidget_currentChanged(self, index):
+ if index == 0: # self.wordview
+ self.wordview.query(self.sentenceview.current_word())
+ self.wordview.query_lineedit.setFocus()
+ elif index == 1: # self.sentenceview
+ self.sentenceview.query(self.wordview.current_word())
+ self.sentenceview.query_lineedit.setFocus()
+
+ def showEvent(self, event):
+ self.show_action.setChecked(True)
- def on_dictview_detailLinkClicked(self, word):
- print 'click mini deatil: ' + word
+ def closeEvent(self, event):
+ self.show_action.setChecked(False)
def main():
app = QApplication(sys.argv)
Please sign in to comment.
Something went wrong with that request. Please try again.