Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sqlitebrowser/src/ExtendedTableWidget.cpp at master · imZack/sqlitebrowser · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
imZack
sqlitebrowser
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
sqlitebrowser
/
src
/
ExtendedTableWidget.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
142 lines (120 loc) · 5.12 KB
master
Breadcrumbs
sqlitebrowser
/
src
/
ExtendedTableWidget.cpp
Copy path
Top
File metadata and controls
Code
Blame
142 lines (120 loc) · 5.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
ExtendedTableWidget.h
"
#
include
"
sqlitetablemodel.h
"
#
include
"
FilterTableHeader.h
"
#
include
"
sqlitetypes.h
"
#
include
<
QApplication
>
#
include
<
QClipboard
>
#
include
<
QKeySequence
>
#
include
<
QKeyEvent
>
#
include
<
QScrollBar
>
#
include
<
QHeaderView
>
ExtendedTableWidget::ExtendedTableWidget
(QWidget* parent) :
QTableView(parent)
{
setHorizontalScrollMode
(ExtendedTableWidget::ScrollPerPixel);
connect
(
verticalScrollBar
(),
SIGNAL
(
valueChanged
(
int
)),
this
,
SLOT
(
vscrollbarChanged
(
int
)));
connect
(
this
,
SIGNAL
(
clicked
(QModelIndex)),
this
,
SLOT
(
cellClicked
(QModelIndex)));
//
Set up filter row
m_tableHeader =
new
FilterTableHeader
(
this
);
setHorizontalHeader
(m_tableHeader);
}
void
ExtendedTableWidget::copy
()
{
//
Get list of selected items
QItemSelectionModel* selection =
selectionModel
();
QModelIndexList indices = selection->
selectedIndexes
();
//
Abort if there's nothing to copy
if
(indices.
size
() ==
0
)
{
return
;
}
else
if
(indices.
size
() ==
1
) {
qApp->
clipboard
()->
setText
(indices.
front
().
data
().
toString
());
return
;
}
//
Sort the items by row, then by column
qSort
(indices);
//
Go through all the items...
QString result;
QModelIndex prev = indices.
front
();
indices.
removeFirst
();
foreach
(QModelIndex index, indices)
{
//
Add the content of this cell to the clipboard string
result.
append
(
QString
(
"
\"
%1
\"
"
).
arg
(prev.
data
().
toString
()));
//
If this is a new row add a line break, if not add a tab for cell separation
if
(index.
row
() != prev.
row
())
result.
append
(
"
\r\n
"
);
else
result.
append
(
"
\t
"
);
prev = index;
}
result.
append
(
QString
(
"
\"
%1
\"\r\n
"
).
arg
(indices.
last
().
data
().
toString
()));
//
And the last cell
//
And finally add it to the clipboard
qApp->
clipboard
()->
setText
(result);
}
void
ExtendedTableWidget::keyPressEvent
(QKeyEvent* event)
{
//
Call a custom copy method when Ctrl-C is pressed
if
(event->
matches
(QKeySequence::Copy))
{
copy
();
}
else
if
(event->
key
() == Qt::Key_Tab &&
hasFocus
() &&
selectedIndexes
().
count
() ==
1
&&
selectedIndexes
().
at
(
0
).
row
() ==
model
()->
rowCount
()-
1
&&
selectedIndexes
().
at
(
0
).
column
() ==
model
()->
columnCount
()-
1
) {
//
If the Tab key was pressed while the focus was on the last cell of the last row insert a new row automatically
model
()->
insertRow
(
model
()->
rowCount
());
}
//
This prevents the current selection from being changed when pressing tab to move to the next filter. Note that this is in an 'if' condition,
//
not in an 'else if' because this way, when the tab key was pressed and the focus was on the last cell, a new row is inserted and then the tab
//
key press is processed a second time to move the cursor as well
if
((event->
key
() != Qt::Key_Tab && event->
key
() != Qt::Key_Backtab) ||
hasFocus
())
QTableView::keyPressEvent
(event);
}
void
ExtendedTableWidget::updateGeometries
()
{
//
Call the parent implementation first - it does most of the actual logic
QTableView::updateGeometries
();
//
Check if a model has already been set yet
if
(
model
())
{
//
If so and if it is a SqliteTableModel and if the parent implementation of this method decided that a scrollbar is needed, update its maximum value
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(
model
());
if
(m &&
verticalScrollBar
()->
maximum
())
verticalScrollBar
()->
setMaximum
(m->
totalRowCount
() -
numVisibleRows
() +
1
);
}
}
void
ExtendedTableWidget::vscrollbarChanged
(
int
value)
{
//
Cancel if there is no model set yet - this shouldn't happen (because without a model there should be no scrollbar) but just to be sure...
if
(!
model
())
return
;
//
Fetch more data from the DB if necessary
if
((value +
numVisibleRows
()) >=
model
()->
rowCount
() &&
model
()->
canFetchMore
(
QModelIndex
()))
model
()->
fetchMore
(
QModelIndex
());
}
int
ExtendedTableWidget::numVisibleRows
()
{
//
Get the row numbers of the rows currently visible at the top and the bottom of the widget
int
row_top =
rowAt
(
0
) == -
1
?
0
:
rowAt
(
0
);
int
row_bottom =
rowAt
(
height
()) == -
1
?
model
()->
rowCount
() :
rowAt
(
height
());
//
Calculate the number of visible rows
return
row_bottom - row_top;
}
QSet<
int
>
ExtendedTableWidget::selectedCols
()
{
QSet<
int
> selectedCols;
foreach
(
const
QModelIndex & idx,
selectedIndexes
())
selectedCols.
insert
(idx.
column
());
return
selectedCols;
}
void
ExtendedTableWidget::cellClicked
(
const
QModelIndex& index)
{
//
If Alt key is pressed try to jump to the row referenced by the foreign key of the clicked cell
if
(qApp->
keyboardModifiers
().
testFlag
(Qt::ControlModifier) && qApp->
keyboardModifiers
().
testFlag
(Qt::ShiftModifier) &&
model
())
{
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(
model
());
sqlb::ForeignKeyClause fk = m->
getForeignKeyClause
(index.
column
()-
1
);
if
(fk.
isSet
())
emit
foreignKeyClicked
(fk.
table
(), fk.
columns
().
size
() ? fk.
columns
().
at
(
0
) :
"
"
, m->
data
(index, Qt::EditRole).
toByteArray
());
}
}
You can’t perform that action at this time.