forked from bnufree/Hq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qkuaixunlistwidget.cpp
135 lines (121 loc) · 4.23 KB
/
qkuaixunlistwidget.cpp
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
#include "qkuaixunlistwidget.h"
#include "ui_qkuaixunlistwidget.h"
#include <QLabel>
#include <QDebug>
#include <QResizeEvent>
#include <QTextBrowser>
#include <QScroller>
#include <QScrollBar>
#include <QDesktopWidget>
#ifdef Q_OS_WIN
//#include <QtWebKitWidgets/QWebView>
#endif
ConfortableLabel::ConfortableLabel(const QString &text, QWidget *parent) : QLabel(text, parent)
{
}
void ConfortableLabel::updateWidth(int width)
{
int total_width = fontMetrics().width(text());
int row = (total_width + width - 1) / width;
setContentsMargins(1,1,1,1);
setMinimumHeight(fontMetrics().height() * row + 5 * (row-1) + 2 );
}
QInfoWidget::QInfoWidget(const KuaixunData &data, QWidget *parent) : QWidget(parent)
{
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
this->setSizePolicy(sizePolicy);
this->setLayout(new QVBoxLayout);
mTitle = new ConfortableLabel(QString("%1 %2").arg(data.src_time).arg(data.sourceString()), this);
mContent = new QTextBrowser(this);
mContent->insertPlainText(data.digest);
mContent->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
layout()->setSpacing(1);
layout()->setContentsMargins(1, 1,1,1);
layout()->addWidget(mTitle);
layout()->addWidget(mContent);
// this->setMinimumWidth(title->width());
}
void QInfoWidget::setReferWidth(int width)
{
setFixedWidth(width);
// mContent->updateWidth(width);
mContent->adjustSize();
setWindowFlags(Qt::SubWindow);
}
QKuaixunListWidget::QKuaixunListWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QKuaixunListWidget)
{
ui->setupUi(this);
// ui->mTextBrowser->setDocumentTitle(QStringLiteral("7*24快讯"));
#ifndef Q_OS_WIN
Qt::GestureType type = QScroller::grabGesture(ui->listWidget);
qDebug()<<"gesture type:"<<type;
#endif
this->setStyleSheet("font-size:16pt;");
ui->listWidget->setFocusPolicy(Qt::NoFocus);
ui->listWidget->setWrapping(false);
ui->listWidget->setWordWrap(true);
ui->listWidget->verticalScrollBar()->setVisible(false);
ui->listWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
ui->listWidget->setResizeMode(QListWidget::Adjust);
ui->listWidget->setSpacing(12);
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(itemDoubleClicked(QListWidgetItem*)));
}
void QKuaixunListWidget::itemDoubleClicked(QListWidgetItem *item)
{
//#ifdef Q_OS_WIN
// if(!item) return;
// KuaixunData data = item->data(Qt::UserRole).value<KuaixunData>();
// QWebView *view = new QWebView;
// view->setUrl(QUrl(data.url));
// view->setWindowFlags(Qt::WindowStaysOnTopHint);
// view->setWindowTitle(data.title);
// view->setAttribute(Qt::WA_DeleteOnClose);
// QRect rect = QApplication::desktop()->availableGeometry();
// QPoint center = rect.center();
// rect.setWidth(rect.width() * 0.5);
// rect.setHeight(rect.height() * 0.5);
// rect.moveCenter(center);
// view->setGeometry(rect);
// view->show();
//#endif
}
QKuaixunListWidget::~QKuaixunListWidget()
{
delete ui;
}
void QKuaixunListWidget::appendData(const KuaiXunList &list)
{
for(int i=list.size()-1; i>=0; i--)
{
KuaixunData data = list.at(i);
QStringList content;
content.append(data.src_time + " " + data.sourceString());
content.first().append("\n");
content.first().append(data.digest);
ui->listWidget->insertItems(0, content);
QListWidgetItem *item = ui->listWidget->item(0);
item->setTextColor(QColor(255, 100,100));
item->setFlags(item->flags()& ~Qt::ItemIsSelectable);
item->setData(Qt::UserRole, QVariant::fromValue(data));
}
int rowCount = ui->listWidget->count();
if(rowCount)
{
ui->listWidget->scrollToTop();
}
int sub_row = rowCount - 100;
while (sub_row > 0) {
ui->listWidget->takeItem(100);
sub_row--;
}
if(!isVisible())
{
emit sendRecvInfoList(list);
}
}