Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
1bc48a78
Commit
1bc48a78
authored
Feb 11, 2014
by
Matt Kelly
Browse files
Convert rest of source to 4-space indent
parent
a171df74
Changes
146
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/pilezone.cpp
View file @
1bc48a78
...
...
@@ -10,109 +10,109 @@
#include
"pb/command_move_card.pb.h"
PileZone
::
PileZone
(
Player
*
_p
,
const
QString
&
_name
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
)
:
CardZone
(
_p
,
_name
,
false
,
_isShufflable
,
_contentsKnown
,
parent
)
:
CardZone
(
_p
,
_name
,
false
,
_isShufflable
,
_contentsKnown
,
parent
)
{
setCacheMode
(
DeviceCoordinateCache
);
// Do not move this line to the parent constructor!
setAcceptsHoverEvents
(
true
);
setCursor
(
Qt
::
OpenHandCursor
);
setTransform
(
QTransform
().
translate
((
float
)
CARD_WIDTH
/
2
,
(
float
)
CARD_HEIGHT
/
2
).
rotate
(
90
).
translate
((
float
)
-
CARD_WIDTH
/
2
,
(
float
)
-
CARD_HEIGHT
/
2
));
setCacheMode
(
DeviceCoordinateCache
);
// Do not move this line to the parent constructor!
setAcceptsHoverEvents
(
true
);
setCursor
(
Qt
::
OpenHandCursor
);
setTransform
(
QTransform
().
translate
((
float
)
CARD_WIDTH
/
2
,
(
float
)
CARD_HEIGHT
/
2
).
rotate
(
90
).
translate
((
float
)
-
CARD_WIDTH
/
2
,
(
float
)
-
CARD_HEIGHT
/
2
));
}
QRectF
PileZone
::
boundingRect
()
const
{
return
QRectF
(
0
,
0
,
CARD_WIDTH
,
CARD_HEIGHT
);
return
QRectF
(
0
,
0
,
CARD_WIDTH
,
CARD_HEIGHT
);
}
void
PileZone
::
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
/*option*/
,
QWidget
*
/*widget*/
)
{
if
(
!
cards
.
isEmpty
())
cards
.
at
(
0
)
->
paintPicture
(
painter
,
cards
.
at
(
0
)
->
getTranslatedSize
(
painter
),
90
);
painter
->
drawRect
(
QRectF
(
0.5
,
0.5
,
CARD_WIDTH
-
1
,
CARD_HEIGHT
-
1
));
painter
->
translate
((
float
)
CARD_WIDTH
/
2
,
(
float
)
CARD_HEIGHT
/
2
);
painter
->
rotate
(
-
90
);
painter
->
translate
((
float
)
-
CARD_WIDTH
/
2
,
(
float
)
-
CARD_HEIGHT
/
2
);
paintNumberEllipse
(
cards
.
size
(),
28
,
Qt
::
white
,
-
1
,
-
1
,
painter
);
if
(
!
cards
.
isEmpty
())
cards
.
at
(
0
)
->
paintPicture
(
painter
,
cards
.
at
(
0
)
->
getTranslatedSize
(
painter
),
90
);
painter
->
drawRect
(
QRectF
(
0.5
,
0.5
,
CARD_WIDTH
-
1
,
CARD_HEIGHT
-
1
));
painter
->
translate
((
float
)
CARD_WIDTH
/
2
,
(
float
)
CARD_HEIGHT
/
2
);
painter
->
rotate
(
-
90
);
painter
->
translate
((
float
)
-
CARD_WIDTH
/
2
,
(
float
)
-
CARD_HEIGHT
/
2
);
paintNumberEllipse
(
cards
.
size
(),
28
,
Qt
::
white
,
-
1
,
-
1
,
painter
);
}
void
PileZone
::
addCardImpl
(
CardItem
*
card
,
int
x
,
int
/*y*/
)
{
connect
(
card
,
SIGNAL
(
sigPixmapUpdated
()),
this
,
SLOT
(
callUpdate
()));
cards
.
insert
(
x
,
card
);
card
->
setPos
(
0
,
0
);
if
(
!
contentsKnown
())
{
card
->
setName
(
QString
());
card
->
setId
(
-
1
);
// If we obscure a previously revealed card, its name has to be forgotten
if
(
cards
.
size
()
>
x
+
1
)
cards
.
at
(
x
+
1
)
->
setName
(
QString
());
}
card
->
setVisible
(
false
);
card
->
resetState
();
card
->
setParentItem
(
this
);
connect
(
card
,
SIGNAL
(
sigPixmapUpdated
()),
this
,
SLOT
(
callUpdate
()));
cards
.
insert
(
x
,
card
);
card
->
setPos
(
0
,
0
);
if
(
!
contentsKnown
())
{
card
->
setName
(
QString
());
card
->
setId
(
-
1
);
// If we obscure a previously revealed card, its name has to be forgotten
if
(
cards
.
size
()
>
x
+
1
)
cards
.
at
(
x
+
1
)
->
setName
(
QString
());
}
card
->
setVisible
(
false
);
card
->
resetState
();
card
->
setParentItem
(
this
);
}
void
PileZone
::
handleDropEvent
(
const
QList
<
CardDragItem
*>
&
dragItems
,
CardZone
*
startZone
,
const
QPoint
&
/*dropPoint*/
)
{
Command_MoveCard
cmd
;
cmd
.
set_start_player_id
(
startZone
->
getPlayer
()
->
getId
());
cmd
.
set_start_zone
(
startZone
->
getName
().
toStdString
());
cmd
.
set_target_player_id
(
player
->
getId
());
cmd
.
set_target_zone
(
getName
().
toStdString
());
cmd
.
set_x
(
0
);
cmd
.
set_y
(
0
);
for
(
int
i
=
0
;
i
<
dragItems
.
size
();
++
i
)
cmd
.
mutable_cards_to_move
()
->
add_card
()
->
set_card_id
(
dragItems
[
i
]
->
getId
());
player
->
sendGameCommand
(
cmd
);
Command_MoveCard
cmd
;
cmd
.
set_start_player_id
(
startZone
->
getPlayer
()
->
getId
());
cmd
.
set_start_zone
(
startZone
->
getName
().
toStdString
());
cmd
.
set_target_player_id
(
player
->
getId
());
cmd
.
set_target_zone
(
getName
().
toStdString
());
cmd
.
set_x
(
0
);
cmd
.
set_y
(
0
);
for
(
int
i
=
0
;
i
<
dragItems
.
size
();
++
i
)
cmd
.
mutable_cards_to_move
()
->
add_card
()
->
set_card_id
(
dragItems
[
i
]
->
getId
());
player
->
sendGameCommand
(
cmd
);
}
void
PileZone
::
reorganizeCards
()
{
update
();
update
();
}
void
PileZone
::
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
)
{
CardZone
::
mousePressEvent
(
event
);
if
(
event
->
isAccepted
())
return
;
if
(
event
->
button
()
==
Qt
::
LeftButton
)
{
setCursor
(
Qt
::
ClosedHandCursor
);
event
->
accept
();
}
else
event
->
ignore
();
CardZone
::
mousePressEvent
(
event
);
if
(
event
->
isAccepted
())
return
;
if
(
event
->
button
()
==
Qt
::
LeftButton
)
{
setCursor
(
Qt
::
ClosedHandCursor
);
event
->
accept
();
}
else
event
->
ignore
();
}
void
PileZone
::
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
)
{
if
((
event
->
screenPos
()
-
event
->
buttonDownScreenPos
(
Qt
::
LeftButton
)).
manhattanLength
()
<
QApplication
::
startDragDistance
())
return
;
if
(
cards
.
isEmpty
())
return
;
bool
faceDown
=
event
->
modifiers
().
testFlag
(
Qt
::
ShiftModifier
);
bool
bottomCard
=
event
->
modifiers
().
testFlag
(
Qt
::
ControlModifier
);
CardItem
*
card
=
bottomCard
?
cards
.
last
()
:
cards
.
first
();
const
int
cardid
=
contentsKnown
()
?
card
->
getId
()
:
(
bottomCard
?
cards
.
size
()
-
1
:
0
);
CardDragItem
*
drag
=
card
->
createDragItem
(
cardid
,
event
->
pos
(),
event
->
scenePos
(),
faceDown
);
drag
->
grabMouse
();
setCursor
(
Qt
::
OpenHandCursor
);
if
((
event
->
screenPos
()
-
event
->
buttonDownScreenPos
(
Qt
::
LeftButton
)).
manhattanLength
()
<
QApplication
::
startDragDistance
())
return
;
if
(
cards
.
isEmpty
())
return
;
bool
faceDown
=
event
->
modifiers
().
testFlag
(
Qt
::
ShiftModifier
);
bool
bottomCard
=
event
->
modifiers
().
testFlag
(
Qt
::
ControlModifier
);
CardItem
*
card
=
bottomCard
?
cards
.
last
()
:
cards
.
first
();
const
int
cardid
=
contentsKnown
()
?
card
->
getId
()
:
(
bottomCard
?
cards
.
size
()
-
1
:
0
);
CardDragItem
*
drag
=
card
->
createDragItem
(
cardid
,
event
->
pos
(),
event
->
scenePos
(),
faceDown
);
drag
->
grabMouse
();
setCursor
(
Qt
::
OpenHandCursor
);
}
void
PileZone
::
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*/
*
event
*/
)
{
setCursor
(
Qt
::
OpenHandCursor
);
setCursor
(
Qt
::
OpenHandCursor
);
}
void
PileZone
::
hoverEnterEvent
(
QGraphicsSceneHoverEvent
*
event
)
{
if
(
!
cards
.
isEmpty
())
cards
[
0
]
->
processHoverEvent
();
QGraphicsItem
::
hoverEnterEvent
(
event
);
if
(
!
cards
.
isEmpty
())
cards
[
0
]
->
processHoverEvent
();
QGraphicsItem
::
hoverEnterEvent
(
event
);
}
cockatrice/src/pilezone.h
View file @
1bc48a78
...
...
@@ -4,21 +4,21 @@
#include
"cardzone.h"
class
PileZone
:
public
CardZone
{
Q_OBJECT
Q_OBJECT
private
slots
:
void
callUpdate
()
{
update
();
}
void
callUpdate
()
{
update
();
}
public:
PileZone
(
Player
*
_p
,
const
QString
&
_name
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
reorganizeCards
();
void
handleDropEvent
(
const
QList
<
CardDragItem
*>
&
dragItems
,
CardZone
*
startZone
,
const
QPoint
&
dropPoint
);
PileZone
(
Player
*
_p
,
const
QString
&
_name
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
reorganizeCards
();
void
handleDropEvent
(
const
QList
<
CardDragItem
*>
&
dragItems
,
CardZone
*
startZone
,
const
QPoint
&
dropPoint
);
protected:
void
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
hoverEnterEvent
(
QGraphicsSceneHoverEvent
*
event
);
void
addCardImpl
(
CardItem
*
card
,
int
x
,
int
y
);
void
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
hoverEnterEvent
(
QGraphicsSceneHoverEvent
*
event
);
void
addCardImpl
(
CardItem
*
card
,
int
x
,
int
y
);
};
#endif
cockatrice/src/pixmapgenerator.cpp
View file @
1bc48a78
...
...
@@ -9,152 +9,152 @@ QMap<QString, QPixmap> PhasePixmapGenerator::pmCache;
QPixmap
PhasePixmapGenerator
::
generatePixmap
(
int
height
,
QString
name
)
{
QString
key
=
name
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/phases/icon_phase_"
+
name
+
".svg"
));
QPixmap
pixmap
(
height
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
height
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
QString
key
=
name
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/phases/icon_phase_"
+
name
+
".svg"
));
QPixmap
pixmap
(
height
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
height
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
}
QMap
<
QString
,
QPixmap
>
CounterPixmapGenerator
::
pmCache
;
QPixmap
CounterPixmapGenerator
::
generatePixmap
(
int
height
,
QString
name
,
bool
highlight
)
{
if
(
highlight
)
name
.
append
(
"_highlight"
);
QString
key
=
name
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/counters/"
+
name
+
".svg"
));
if
(
!
svg
.
isValid
())
{
name
=
"general"
;
if
(
highlight
)
name
.
append
(
"_highlight"
);
svg
.
load
(
QString
(
":/resources/counters/"
+
name
+
".svg"
));
}
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
if
(
highlight
)
name
.
append
(
"_highlight"
);
QString
key
=
name
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/counters/"
+
name
+
".svg"
));
if
(
!
svg
.
isValid
())
{
name
=
"general"
;
if
(
highlight
)
name
.
append
(
"_highlight"
);
svg
.
load
(
QString
(
":/resources/counters/"
+
name
+
".svg"
));
}
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
}
QPixmap
PingPixmapGenerator
::
generatePixmap
(
int
size
,
int
value
,
int
max
)
{
int
key
=
size
*
1000000
+
max
*
1000
+
value
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QPixmap
pixmap
(
size
,
size
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
QColor
color
;
if
((
max
==
-
1
)
||
(
value
==
-
1
))
color
=
Qt
::
black
;
else
color
.
setHsv
(
120
*
(
1.0
-
((
double
)
value
/
max
)),
255
,
255
);
QRadialGradient
g
(
QPointF
((
double
)
pixmap
.
width
()
/
2
,
(
double
)
pixmap
.
height
()
/
2
),
qMin
(
pixmap
.
width
(),
pixmap
.
height
())
/
2.0
);
g
.
setColorAt
(
0
,
color
);
g
.
setColorAt
(
1
,
Qt
::
transparent
);
painter
.
fillRect
(
0
,
0
,
pixmap
.
width
(),
pixmap
.
height
(),
QBrush
(
g
));
pmCache
.
insert
(
key
,
pixmap
);
int
key
=
size
*
1000000
+
max
*
1000
+
value
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QPixmap
pixmap
(
size
,
size
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
QColor
color
;
if
((
max
==
-
1
)
||
(
value
==
-
1
))
color
=
Qt
::
black
;
else
color
.
setHsv
(
120
*
(
1.0
-
((
double
)
value
/
max
)),
255
,
255
);
QRadialGradient
g
(
QPointF
((
double
)
pixmap
.
width
()
/
2
,
(
double
)
pixmap
.
height
()
/
2
),
qMin
(
pixmap
.
width
(),
pixmap
.
height
())
/
2.0
);
g
.
setColorAt
(
0
,
color
);
g
.
setColorAt
(
1
,
Qt
::
transparent
);
painter
.
fillRect
(
0
,
0
,
pixmap
.
width
(),
pixmap
.
height
(),
QBrush
(
g
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
return
pixmap
;
}
QMap
<
int
,
QPixmap
>
PingPixmapGenerator
::
pmCache
;
QPixmap
GenderPixmapGenerator
::
generatePixmap
(
int
height
,
int
_gender
)
{
ServerInfo_User
::
Gender
gender
=
static_cast
<
ServerInfo_User
::
Gender
>
(
_gender
);
if
((
gender
!=
ServerInfo_User
::
Male
)
&&
(
gender
!=
ServerInfo_User
::
Female
))
gender
=
ServerInfo_User
::
GenderUnknown
;
int
key
=
gender
*
100000
+
height
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QString
genderStr
;
switch
(
gender
)
{
case
ServerInfo_User
::
Male
:
genderStr
=
"male"
;
break
;
case
ServerInfo_User
::
Female
:
genderStr
=
"female"
;
break
;
default:
genderStr
=
"unknown"
;
};
QSvgRenderer
svg
(
QString
(
":/resources/genders/"
+
genderStr
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
ServerInfo_User
::
Gender
gender
=
static_cast
<
ServerInfo_User
::
Gender
>
(
_gender
);
if
((
gender
!=
ServerInfo_User
::
Male
)
&&
(
gender
!=
ServerInfo_User
::
Female
))
gender
=
ServerInfo_User
::
GenderUnknown
;
int
key
=
gender
*
100000
+
height
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QString
genderStr
;
switch
(
gender
)
{
case
ServerInfo_User
::
Male
:
genderStr
=
"male"
;
break
;
case
ServerInfo_User
::
Female
:
genderStr
=
"female"
;
break
;
default:
genderStr
=
"unknown"
;
};
QSvgRenderer
svg
(
QString
(
":/resources/genders/"
+
genderStr
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
}
QMap
<
int
,
QPixmap
>
GenderPixmapGenerator
::
pmCache
;
QPixmap
CountryPixmapGenerator
::
generatePixmap
(
int
height
,
const
QString
&
countryCode
)
{
if
(
countryCode
.
size
()
!=
2
)
return
QPixmap
();
QString
key
=
countryCode
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/countries/"
+
countryCode
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
painter
.
setPen
(
Qt
::
black
);
painter
.
drawRect
(
0
,
0
,
width
-
1
,
height
-
1
);
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
if
(
countryCode
.
size
()
!=
2
)
return
QPixmap
();
QString
key
=
countryCode
+
QString
::
number
(
height
);
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QSvgRenderer
svg
(
QString
(
":/resources/countries/"
+
countryCode
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
painter
.
setPen
(
Qt
::
black
);
painter
.
drawRect
(
0
,
0
,
width
-
1
,
height
-
1
);
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
}
QMap
<
QString
,
QPixmap
>
CountryPixmapGenerator
::
pmCache
;
QPixmap
UserLevelPixmapGenerator
::
generatePixmap
(
int
height
,
UserLevelFlags
userLevel
)
{
int
key
=
height
*
10000
+
(
int
)
userLevel
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QString
levelString
;
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsAdmin
))
levelString
=
"admin"
;
else
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsModerator
))
levelString
=
"moderator"
;
else
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsRegistered
))
levelString
=
"registered"
;
else
levelString
=
"normal"
;
QSvgRenderer
svg
(
QString
(
":/resources/userlevels/"
+
levelString
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
int
key
=
height
*
10000
+
(
int
)
userLevel
;
if
(
pmCache
.
contains
(
key
))
return
pmCache
.
value
(
key
);
QString
levelString
;
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsAdmin
))
levelString
=
"admin"
;
else
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsModerator
))
levelString
=
"moderator"
;
else
if
(
userLevel
.
testFlag
(
ServerInfo_User
::
IsRegistered
))
levelString
=
"registered"
;
else
levelString
=
"normal"
;
QSvgRenderer
svg
(
QString
(
":/resources/userlevels/"
+
levelString
+
".svg"
));
int
width
=
(
int
)
round
(
height
*
(
double
)
svg
.
defaultSize
().
width
()
/
(
double
)
svg
.
defaultSize
().
height
());
QPixmap
pixmap
(
width
,
height
);
pixmap
.
fill
(
Qt
::
transparent
);
QPainter
painter
(
&
pixmap
);
svg
.
render
(
&
painter
,
QRectF
(
0
,
0
,
width
,
height
));
pmCache
.
insert
(
key
,
pixmap
);
return
pixmap
;
}
QMap
<
int
,
QPixmap
>
UserLevelPixmapGenerator
::
pmCache
;
cockatrice/src/pixmapgenerator.h
View file @
1bc48a78
...
...
@@ -8,50 +8,50 @@
class
PhasePixmapGenerator
{
private:
static
QMap
<
QString
,
QPixmap
>
pmCache
;
static
QMap
<
QString
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
size
,
QString
name
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
size
,
QString
name
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
class
CounterPixmapGenerator
{
private:
static
QMap
<
QString
,
QPixmap
>
pmCache
;
static
QMap
<
QString
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
size
,
QString
name
,
bool
highlight
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
size
,
QString
name
,
bool
highlight
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
class
PingPixmapGenerator
{
private:
static
QMap
<
int
,
QPixmap
>
pmCache
;
static
QMap
<
int
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
size
,
int
value
,
int
max
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
size
,
int
value
,
int
max
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
class
GenderPixmapGenerator
{
private:
static
QMap
<
int
,
QPixmap
>
pmCache
;
static
QMap
<
int
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
height
,
int
gender
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
height
,
int
gender
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
class
CountryPixmapGenerator
{
private:
static
QMap
<
QString
,
QPixmap
>
pmCache
;
static
QMap
<
QString
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
height
,
const
QString
&
countryCode
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
height
,
const
QString
&
countryCode
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
class
UserLevelPixmapGenerator
{
private:
static
QMap
<
int
,
QPixmap
>
pmCache
;
static
QMap
<
int
,
QPixmap
>
pmCache
;
public:
static
QPixmap
generatePixmap
(
int
height
,
UserLevelFlags
userLevel
);
static
void
clear
()
{
pmCache
.
clear
();
}
static
QPixmap
generatePixmap
(
int
height
,
UserLevelFlags
userLevel
);
static
void
clear
()
{
pmCache
.
clear
();
}
};
#endif
cockatrice/src/player.h
View file @
1bc48a78
...
...
@@ -57,246 +57,246 @@ class Event_ChangeZoneProperties;
class
PendingCommand
;
class
PlayerArea
:
public
QObject
,
public
QGraphicsItem
{
Q_OBJECT
private:
QBrush
bgPixmapBrush
;
QRectF
bRect
;
Q_OBJECT
private:
QBrush
bgPixmapBrush
;
QRectF
bRect
;
private
slots
:
void
updateBgPixmap
();
void
updateBgPixmap
();
public:
enum
{
Type
=
typeOther
};
int
type
()
const
{
return
Type
;
}
PlayerArea
(
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
{
return
bRect
;
}
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
setSize
(
qreal
width
,
qreal
height
);
enum
{
Type
=
typeOther
};
int
type
()
const
{
return
Type
;
}
PlayerArea
(
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
{
return
bRect
;
}
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
setSize
(
qreal
width
,
qreal
height
);
};
class
Player
:
public
QObject
,
public
QGraphicsItem
{
Q_OBJECT
Q_OBJECT
signals:
void
openDeckEditor
(
const
DeckLoader
*
deck
);
void
newCardAdded
(
AbstractCardItem
*
card
);
// Log events
void
logSay
(
Player
*
player
,
QString
message
);
void
logShuffle
(
Player
*
player
,
CardZone
*
zone
);
void
logRollDie
(
Player
*
player
,
int
sides
,
int
roll
);
void
logCreateArrow
(
Player
*
player
,
Player
*
startPlayer
,
QString
startCard
,
Player
*
targetPlayer
,
QString
targetCard
,
bool
_playerTarget
);
void
logCreateToken
(
Player
*
player
,
QString
cardName
,
QString
pt
);
void
logDrawCards
(
Player
*
player
,
int
number
);
void
logUndoDraw
(
Player
*
player
,
QString
cardName
);
void
logMoveCard
(
Player
*
player
,
CardItem
*
card
,
CardZone
*
startZone
,
int
oldX
,
CardZone
*
targetZone
,
int
newX
);
void
logFlipCard
(
Player
*
player
,
QString
cardName
,
bool
faceDown
);
void
logDestroyCard
(
Player
*
player
,
QString
cardName
);
void
logAttachCard
(
Player
*
player
,
QString
cardName
,
Player
*
targetPlayer
,
QString
targetCardName
);
void
logUnattachCard
(
Player
*
player
,
QString
cardName
);
void
logSetCardCounter
(
Player
*
player
,
QString
cardName
,
int
counterId
,
int
value
,
int
oldValue
);
void
logSetTapped
(
Player
*
player
,
CardItem
*
card
,
bool
tapped
);
void
logSetCounter
(
Player
*
player
,
QString
counterName
,
int
value
,
int
oldValue
);
void
logSetDoesntUntap
(
Player
*
player
,
CardItem
*
card
,
bool
doesntUntap
);
void
logSetPT
(
Player
*
player
,
CardItem
*
card
,
QString
newPT
);
void
logSetAnnotation
(
Player
*
player
,
CardItem
*
card
,
QString
newAnnotation
);
void
logDumpZone
(
Player
*
player
,
CardZone
*
zone
,
int
numberCards
);
void
logStopDumpZone
(
Player
*
player
,
CardZone
*
zone
);
void
logRevealCards
(
Player
*
player
,
CardZone
*
zone
,
int
cardId
,
QString
cardName
,
Player
*
otherPlayer
,
bool
faceDown
);
void
logAlwaysRevealTopCard
(
Player
*
player
,
CardZone
*
zone
,
bool
reveal
);
void
sizeChanged
();
void
gameConceded
();
void
openDeckEditor
(
const
DeckLoader
*
deck
);
void
newCardAdded
(
AbstractCardItem
*
card
);
// Log events
void
logSay
(
Player
*
player
,
QString
message
);
void
logShuffle
(
Player
*
player
,
CardZone
*
zone
);
void
logRollDie
(
Player
*
player
,
int
sides
,
int
roll
);
void
logCreateArrow
(
Player
*
player
,
Player
*
startPlayer
,
QString
startCard
,
Player
*
targetPlayer
,
QString
targetCard
,
bool
_playerTarget
);
void
logCreateToken
(
Player
*
player
,
QString
cardName
,
QString
pt
);
void
logDrawCards
(
Player
*
player
,
int
number
);
void
logUndoDraw
(
Player
*
player
,
QString
cardName
);
void
logMoveCard
(
Player
*
player
,
CardItem
*
card
,
CardZone
*
startZone
,
int
oldX
,
CardZone
*
targetZone
,
int
newX
);
void
logFlipCard
(
Player
*
player
,
QString
cardName
,
bool
faceDown
);
void
logDestroyCard
(
Player
*
player
,
QString
cardName
);
void
logAttachCard
(
Player
*
player
,
QString
cardName
,
Player
*
targetPlayer
,
QString
targetCardName
);
void
logUnattachCard
(
Player
*
player
,
QString
cardName
);
void
logSetCardCounter
(
Player
*
player
,
QString
cardName
,
int
counterId
,
int
value
,
int
oldValue
);
void
logSetTapped
(
Player
*
player
,
CardItem
*
card
,
bool
tapped
);
void
logSetCounter
(
Player
*
player
,
QString
counterName
,
int
value
,
int
oldValue
);
void
logSetDoesntUntap
(
Player
*
player
,
CardItem
*
card
,
bool
doesntUntap
);
void
logSetPT
(
Player
*
player
,
CardItem
*
card
,
QString
newPT
);
void
logSetAnnotation
(
Player
*
player
,
CardItem
*
card
,
QString
newAnnotation
);
void
logDumpZone
(
Player
*
player
,
CardZone
*
zone
,
int
numberCards
);
void
logStopDumpZone
(
Player
*
player
,
CardZone
*
zone
);
void
logRevealCards
(
Player
*
player
,
CardZone
*
zone
,
int
cardId
,
QString
cardName
,
Player
*
otherPlayer
,
bool
faceDown
);
void
logAlwaysRevealTopCard
(
Player
*
player
,
CardZone
*
zone
,
bool
reveal
);
void
sizeChanged
();
void
gameConceded
();
public
slots
:
void
actUntapAll
();
void
actRollDie
();
void
actCreateToken
();
void
actCreateAnotherToken
();
void
actShuffle
();
void
actDrawCard
();
void
actDrawCards
();
void
actUndoDraw
();
void
actUntapAll
();
void
actRollDie
();
void
actCreateToken
();
void
actCreateAnotherToken
();
void
actShuffle
();
void
actDrawCard
();
void
actDrawCards
();
void
actUndoDraw
();
void
actMulligan
();
void
actMoveTopCardsToGrave
();
void
actMoveTopCardsToExile
();
void
actMoveTopCardToBottom
();
void
actMoveTopCardsToGrave
();
void
actMoveTopCardsToExile
();
void
actMoveTopCardToBottom
();
void
actViewLibrary
();
void
actViewTopCards
();
void
actAlwaysRevealTopCard
();
void
actViewGraveyard
();
void
actViewRfg
();
void
actViewSideboard
();
void
actSayMessage
();
void
actViewLibrary
();
void
actViewTopCards
();
void
actAlwaysRevealTopCard
();
void
actViewGraveyard
();
void
actViewRfg
();
void
actViewSideboard
();
void
actSayMessage
();
private
slots
:
void
addPlayer
(
Player
*
player
);
void
removePlayer
(
Player
*
player
);
void
playerListActionTriggered
();
void
updateBoundingRect
();
void
rearrangeZones
();
void
actOpenDeckInDeckEditor
();
void
actCreatePredefinedToken
();
void
cardMenuAction
();
void
actCardCounterTrigger
();
void
actAttach
();
void
actUnattach
();
void
actDrawArrow
();
void
actIncPT
(
int
deltaP
,
int
deltaT
);
void
actSetPT
();
void
actIncP
();
void
actDecP
();
void
actIncT
();
void
actDecT
();
void
actIncPT
();
void
actDecPT
();
void
actSetAnnotation
();
void
actPlay
();
void
actHide
();
void
addPlayer
(
Player
*
player
);
void
removePlayer
(
Player
*
player
);
void
playerListActionTriggered
();
void
updateBoundingRect
();
void
rearrangeZones
();
void
actOpenDeckInDeckEditor
();
void
actCreatePredefinedToken
();
void
cardMenuAction
();
void
actCardCounterTrigger
();
void
actAttach
();
void
actUnattach
();
void
actDrawArrow
();
void
actIncPT
(
int
deltaP
,
int
deltaT
);
void
actSetPT
();
void
actIncP
();
void
actDecP
();
void
actIncT
();
void
actDecT
();
void
actIncPT
();
void
actDecPT
();
void
actSetAnnotation
();
void
actPlay
();
void
actHide
();
private:
TabGame
*
game
;
QMenu
*
playerMenu
,
*
handMenu
,
*
graveMenu
,
*
rfgMenu
,
*
libraryMenu
,
*
sbMenu
,
*
countersMenu
,
*
sayMenu
,
*
createPredefinedTokenMenu
,
*
mRevealLibrary
,
*
mRevealTopCard
,
*
mRevealHand
,
*
mRevealRandomHandCard
;
QList
<
QMenu
*>
playerLists
;
QList
<
QAction
*>
allPlayersActions
;
QAction
*
aMoveHandToTopLibrary
,
*
aMoveHandToBottomLibrary
,
*
aMoveHandToGrave
,
*
aMoveHandToRfg
,
*
aMoveGraveToTopLibrary
,
*
aMoveGraveToBottomLibrary
,
*
aMoveGraveToHand
,
*
aMoveGraveToRfg
,
*
aMoveRfgToTopLibrary
,
*
aMoveRfgToBottomLibrary
,
*
aMoveRfgToHand
,
*
aMoveRfgToGrave
,
*
aViewLibrary
,
*
aViewTopCards
,
*
aAlwaysRevealTopCard
,
*
aOpenDeckInDeckEditor
,
*
aMoveTopCardsToGrave
,
*
aMoveTopCardsToExile
,
*
aMoveTopCardToBottom
,
*
aViewGraveyard
,
*
aViewRfg
,
*
aViewSideboard
,
TabGame
*
game
;
QMenu
*
playerMenu
,
*
handMenu
,
*
graveMenu
,
*
rfgMenu
,
*
libraryMenu
,
*
sbMenu
,
*
countersMenu
,
*
sayMenu
,
*
createPredefinedTokenMenu
,
*
mRevealLibrary
,
*
mRevealTopCard
,
*
mRevealHand
,
*
mRevealRandomHandCard
;
QList
<
QMenu
*>
playerLists
;
QList
<
QAction
*>
allPlayersActions
;
QAction
*
aMoveHandToTopLibrary
,
*
aMoveHandToBottomLibrary
,
*
aMoveHandToGrave
,
*
aMoveHandToRfg
,
*
aMoveGraveToTopLibrary
,
*
aMoveGraveToBottomLibrary
,
*
aMoveGraveToHand
,
*
aMoveGraveToRfg
,
*
aMoveRfgToTopLibrary
,
*
aMoveRfgToBottomLibrary
,
*
aMoveRfgToHand
,
*
aMoveRfgToGrave
,
*
aViewLibrary
,
*
aViewTopCards
,
*
aAlwaysRevealTopCard
,
*
aOpenDeckInDeckEditor
,
*
aMoveTopCardsToGrave
,
*
aMoveTopCardsToExile
,
*
aMoveTopCardToBottom
,
*
aViewGraveyard
,
*
aViewRfg
,
*
aViewSideboard
,
*
aDrawCard
,
*
aDrawCards
,
*
aUndoDraw
,
*
aMulligan
,
*
aShuffle
,
*
aUntapAll
,
*
aRollDie
,
*
aCreateToken
,
*
aCreateAnotherToken
,
*
aCardMenu
;
QList
<
QAction
*>
aAddCounter
,
aSetCounter
,
aRemoveCounter
;
QAction
*
aPlay
,
*
aHide
,
*
aTap
,
*
aUntap
,
*
aDoesntUntap
,
*
aAttach
,
*
aUnattach
,
*
aDrawArrow
,
*
aSetPT
,
*
aIncP
,
*
aDecP
,
*
aIncT
,
*
aDecT
,
*
aIncPT
,
*
aDecPT
,
*
aSetAnnotation
,
*
aFlip
,
*
aPeek
,
*
aClone
,
*
aMoveToTopLibrary
,
*
aMoveToBottomLibrary
,
*
aMoveToGraveyard
,
*
aMoveToExile
;
*
aUntapAll
,
*
aRollDie
,
*
aCreateToken
,
*
aCreateAnotherToken
,
*
aCardMenu
;
QList
<
QAction
*>
aAddCounter
,
aSetCounter
,
aRemoveCounter
;
QAction
*
aPlay
,
*
aHide
,
*
aTap
,
*
aUntap
,
*
aDoesntUntap
,
*
aAttach
,
*
aUnattach
,
*
aDrawArrow
,
*
aSetPT
,
*
aIncP
,
*
aDecP
,
*
aIncT
,
*
aDecT
,
*
aIncPT
,
*
aDecPT
,
*
aSetAnnotation
,
*
aFlip
,
*
aPeek
,
*
aClone
,
*
aMoveToTopLibrary
,
*
aMoveToBottomLibrary
,
*
aMoveToGraveyard
,
*
aMoveToExile
;
bool
shortcutsActive
;
int
defaultNumberTopCards
;
QString
lastTokenName
,
lastTokenColor
,
lastTokenPT
,
lastTokenAnnotation
;
bool
lastTokenDestroy
;
ServerInfo_User
*
userInfo
;
int
id
;
bool
active
;
bool
local
;
bool
mirrored
;
bool
handVisible
;
bool
conceded
;
bool
dialogSemaphore
;
bool
clearCardsToDelete
();
QList
<
CardItem
*>
cardsToDelete
;
DeckLoader
*
deck
;
QStringList
predefinedTokens
;
PlayerArea
*
playerArea
;
QMap
<
QString
,
CardZone
*>
zones
;
StackZone
*
stack
;
TableZone
*
table
;
HandZone
*
hand
;
PlayerTarget
*
playerTarget
;
void
setCardAttrHelper
(
const
GameEventContext
&
context
,
CardItem
*
card
,
CardAttribute
attribute
,
const
QString
&
avalue
,
bool
allCards
);
bool
shortcutsActive
;
int
defaultNumberTopCards
;
QString
lastTokenName
,
lastTokenColor
,
lastTokenPT
,
lastTokenAnnotation
;
bool
lastTokenDestroy
;
ServerInfo_User
*
userInfo
;
int
id
;
bool
active
;
bool
local
;
bool
mirrored
;
bool
handVisible
;
bool
conceded
;
bool
dialogSemaphore
;
bool
clearCardsToDelete
();
QList
<
CardItem
*>
cardsToDelete
;
DeckLoader
*
deck
;
QStringList
predefinedTokens
;
PlayerArea
*
playerArea
;
QMap
<
QString
,
CardZone
*>
zones
;
StackZone
*
stack
;
TableZone
*
table
;
HandZone
*
hand
;
PlayerTarget
*
playerTarget
;
void
setCardAttrHelper
(
const
GameEventContext
&
context
,
CardItem
*
card
,
CardAttribute
attribute
,
const
QString
&
avalue
,
bool
allCards
);
QRectF
bRect
;
QRectF
bRect
;
QMap
<
int
,
AbstractCounter
*>
counters
;
QMap
<
int
,
ArrowItem
*>
arrows
;
void
rearrangeCounters
();
void
initSayMenu
();
void
eventConnectionStateChanged
(
const
Event_ConnectionStateChanged
&
event
);
void
eventGameSay
(
const
Event_GameSay
&
event
);
void
eventShuffle
(
const
Event_Shuffle
&
event
);
void
eventRollDie
(
const
Event_RollDie
&
event
);
void
eventCreateArrow
(
const
Event_CreateArrow
&
event
);
void
eventDeleteArrow
(
const
Event_DeleteArrow
&
event
);
void
eventCreateToken
(
const
Event_CreateToken
&
event
);
void
eventSetCardAttr
(
const
Event_SetCardAttr
&
event
,
const
GameEventContext
&
context
);
void
eventSetCardCounter
(
const
Event_SetCardCounter
&
event
);
void
eventCreateCounter
(
const
Event_CreateCounter
&
event
);
void
eventSetCounter
(
const
Event_SetCounter
&
event
);
void
eventDelCounter
(
const
Event_DelCounter
&
event
);
void
eventDumpZone
(
const
Event_DumpZone
&
event
);
void
eventStopDumpZone
(
const
Event_StopDumpZone
&
event
);
void
eventMoveCard
(
const
Event_MoveCard
&
event
,
const
GameEventContext
&
context
);
void
eventFlipCard
(
const
Event_FlipCard
&
event
);
void
eventDestroyCard
(
const
Event_DestroyCard
&
event
);
void
eventAttachCard
(
const
Event_AttachCard
&
event
);
void
eventDrawCards
(
const
Event_DrawCards
&
event
);
void
eventRevealCards
(
const
Event_RevealCards
&
event
);
void
eventChangeZoneProperties
(
const
Event_ChangeZoneProperties
&
event
);
QMap
<
int
,
AbstractCounter
*>
counters
;
QMap
<
int
,
ArrowItem
*>
arrows
;
void
rearrangeCounters
();
void
initSayMenu
();
void
eventConnectionStateChanged
(
const
Event_ConnectionStateChanged
&
event
);
void
eventGameSay
(
const
Event_GameSay
&
event
);
void
eventShuffle
(
const
Event_Shuffle
&
event
);
void
eventRollDie
(
const
Event_RollDie
&
event
);
void
eventCreateArrow
(
const
Event_CreateArrow
&
event
);
void
eventDeleteArrow
(
const
Event_DeleteArrow
&
event
);
void
eventCreateToken
(
const
Event_CreateToken
&
event
);
void
eventSetCardAttr
(
const
Event_SetCardAttr
&
event
,
const
GameEventContext
&
context
);
void
eventSetCardCounter
(
const
Event_SetCardCounter
&
event
);
void
eventCreateCounter
(
const
Event_CreateCounter
&
event
);
void
eventSetCounter
(
const
Event_SetCounter
&
event
);
void
eventDelCounter
(
const
Event_DelCounter
&
event
);
void
eventDumpZone
(
const
Event_DumpZone
&
event
);
void
eventStopDumpZone
(
const
Event_StopDumpZone
&
event
);
void
eventMoveCard
(
const
Event_MoveCard
&
event
,
const
GameEventContext
&
context
);
void
eventFlipCard
(
const
Event_FlipCard
&
event
);
void
eventDestroyCard
(
const
Event_DestroyCard
&
event
);
void
eventAttachCard
(
const
Event_AttachCard
&
event
);
void
eventDrawCards
(
const
Event_DrawCards
&
event
);
void
eventRevealCards
(
const
Event_RevealCards
&
event
);
void
eventChangeZoneProperties
(
const
Event_ChangeZoneProperties
&
event
);
public:
static
const
int
counterAreaWidth
=
55
;
enum
CardMenuActionType
{
cmTap
,
cmUntap
,
cmDoesntUntap
,
cmFlip
,
cmPeek
,
cmClone
,
cmMoveToTopLibrary
,
cmMoveToBottomLibrary
,
cmMoveToGraveyard
,
cmMoveToExile
};
enum
{
Type
=
typeOther
};
int
type
()
const
{
return
Type
;
}
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
playCard
(
CardItem
*
c
,
bool
faceDown
,
bool
tapped
);
void
addCard
(
CardItem
*
c
);
void
deleteCard
(
CardItem
*
c
);
void
addZone
(
CardZone
*
z
);
static
const
int
counterAreaWidth
=
55
;
enum
CardMenuActionType
{
cmTap
,
cmUntap
,
cmDoesntUntap
,
cmFlip
,
cmPeek
,
cmClone
,
cmMoveToTopLibrary
,
cmMoveToBottomLibrary
,
cmMoveToGraveyard
,
cmMoveToExile
};
enum
{
Type
=
typeOther
};
int
type
()
const
{
return
Type
;
}
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
void
playCard
(
CardItem
*
c
,
bool
faceDown
,
bool
tapped
);
void
addCard
(
CardItem
*
c
);
void
deleteCard
(
CardItem
*
c
);
void
addZone
(
CardZone
*
z
);
AbstractCounter
*
addCounter
(
const
ServerInfo_Counter
&
counter
);
AbstractCounter
*
addCounter
(
int
counterId
,
const
QString
&
name
,
QColor
color
,
int
radius
,
int
value
);
void
delCounter
(
int
counterId
);
void
clearCounters
();
ArrowItem
*
addArrow
(
const
ServerInfo_Arrow
&
arrow
);
ArrowItem
*
addArrow
(
int
arrowId
,
CardItem
*
startCard
,
ArrowTarget
*
targetItem
,
const
QColor
&
color
);
void
delArrow
(
int
arrowId
);
void
removeArrow
(
ArrowItem
*
arrow
);
void
clearArrows
();
PlayerTarget
*
getPlayerTarget
()
const
{
return
playerTarget
;
}
AbstractCounter
*
addCounter
(
const
ServerInfo_Counter
&
counter
);
AbstractCounter
*
addCounter
(
int
counterId
,
const
QString
&
name
,
QColor
color
,
int
radius
,
int
value
);
void
delCounter
(
int
counterId
);
void
clearCounters
();
ArrowItem
*
addArrow
(
const
ServerInfo_Arrow
&
arrow
);
ArrowItem
*
addArrow
(
int
arrowId
,
CardItem
*
startCard
,
ArrowTarget
*
targetItem
,
const
QColor
&
color
);
void
delArrow
(
int
arrowId
);
void
removeArrow
(
ArrowItem
*
arrow
);
void
clearArrows
();
PlayerTarget
*
getPlayerTarget
()
const
{
return
playerTarget
;
}
Player
(
const
ServerInfo_User
&
info
,
int
_id
,
bool
_local
,
TabGame
*
_parent
);
~
Player
();
void
retranslateUi
();
void
clear
();
TabGame
*
getGame
()
const
{
return
game
;
}
void
setDeck
(
const
DeckLoader
&
_deck
);
QMenu
*
getPlayerMenu
()
const
{
return
playerMenu
;
}
int
getId
()
const
{
return
id
;
}
QString
getName
()
const
;
ServerInfo_User
*
getUserInfo
()
const
{
return
userInfo
;
}
bool
getLocal
()
const
{
return
local
;
}
bool
getMirrored
()
const
{
return
mirrored
;
}
const
QMap
<
QString
,
CardZone
*>
&
getZones
()
const
{
return
zones
;
}
const
QMap
<
int
,
ArrowItem
*>
&
getArrows
()
const
{
return
arrows
;
}
void
setCardMenu
(
QMenu
*
menu
);
QMenu
*
getCardMenu
()
const
;
void
updateCardMenu
(
CardItem
*
card
);
bool
getActive
()
const
{
return
active
;
}
void
setActive
(
bool
_active
);
void
setShortcutsActive
();
void
setShortcutsInactive
();
void
updateZones
();
void
setConceded
(
bool
_conceded
);
bool
getConceded
()
const
{
return
conceded
;
}
qreal
getMinimumWidth
()
const
;
void
setMirrored
(
bool
_mirrored
);
void
processSceneSizeChange
(
int
newPlayerWidth
);
void
processPlayerInfo
(
const
ServerInfo_Player
&
info
);
void
processCardAttachment
(
const
ServerInfo_Player
&
info
);
void
processGameEvent
(
GameEvent
::
GameEventType
type
,
const
GameEvent
&
event
,
const
GameEventContext
&
context
);
Player
(
const
ServerInfo_User
&
info
,
int
_id
,
bool
_local
,
TabGame
*
_parent
);
~
Player
();
void
retranslateUi
();
void
clear
();
TabGame
*
getGame
()
const
{
return
game
;
}
void
setDeck
(
const
DeckLoader
&
_deck
);
QMenu
*
getPlayerMenu
()
const
{
return
playerMenu
;
}
int
getId
()
const
{
return
id
;
}
QString
getName
()
const
;
ServerInfo_User
*
getUserInfo
()
const
{
return
userInfo
;
}
bool
getLocal
()
const
{
return
local
;
}
bool
getMirrored
()
const
{
return
mirrored
;
}
const
QMap
<
QString
,
CardZone
*>
&
getZones
()
const
{
return
zones
;
}
const
QMap
<
int
,
ArrowItem
*>
&
getArrows
()
const
{
return
arrows
;
}
void
setCardMenu
(
QMenu
*
menu
);
QMenu
*
getCardMenu
()
const
;
void
updateCardMenu
(
CardItem
*
card
);
bool
getActive
()
const
{
return
active
;
}
void
setActive
(
bool
_active
);
void
setShortcutsActive
();
void
setShortcutsInactive
();
void
updateZones
();
void
setConceded
(
bool
_conceded
);
bool
getConceded
()
const
{
return
conceded
;
}
qreal
getMinimumWidth
()
const
;
void
setMirrored
(
bool
_mirrored
);
void
processSceneSizeChange
(
int
newPlayerWidth
);
void
processPlayerInfo
(
const
ServerInfo_Player
&
info
);
void
processCardAttachment
(
const
ServerInfo_Player
&
info
);
void
processGameEvent
(
GameEvent
::
GameEventType
type
,
const
GameEvent
&
event
,
const
GameEventContext
&
context
);
PendingCommand
*
prepareGameCommand
(
const
::
google
::
protobuf
::
Message
&
cmd
);
PendingCommand
*
prepareGameCommand
(
const
QList
<
const
::
google
::
protobuf
::
Message
*
>
&
cmdList
);
void
sendGameCommand
(
PendingCommand
*
pend
);
void
sendGameCommand
(
const
google
::
protobuf
::
Message
&
command
);
PendingCommand
*
prepareGameCommand
(
const
::
google
::
protobuf
::
Message
&
cmd
);
PendingCommand
*
prepareGameCommand
(
const
QList
<
const
::
google
::
protobuf
::
Message
*
>
&
cmdList
);
void
sendGameCommand
(
PendingCommand
*
pend
);
void
sendGameCommand
(
const
google
::
protobuf
::
Message
&
command
);
};
#endif
cockatrice/src/playerlistwidget.cpp
View file @
1bc48a78
...
...
@@ -17,63 +17,63 @@
#include
"pb/serverinfo_playerproperties.pb.h"
PlayerListItemDelegate
::
PlayerListItemDelegate
(
QObject
*
const
parent
)
:
QStyledItemDelegate
(
parent
)
:
QStyledItemDelegate
(
parent
)
{
}
bool
PlayerListItemDelegate
::
editorEvent
(
QEvent
*
event
,
QAbstractItemModel
*
model
,
const
QStyleOptionViewItem
&
option
,
const
QModelIndex
&
index
)
{
if
((
event
->
type
()
==
QEvent
::
MouseButtonPress
)
&&
index
.
isValid
())
{
QMouseEvent
*
const
mouseEvent
=
static_cast
<
QMouseEvent
*>
(
event
);
if
(
mouseEvent
->
button
()
==
Qt
::
RightButton
)
{
static_cast
<
PlayerListWidget
*>
(
parent
())
->
showContextMenu
(
mouseEvent
->
globalPos
(),
index
);
return
true
;
}
}
return
QStyledItemDelegate
::
editorEvent
(
event
,
model
,
option
,
index
);
if
((
event
->
type
()
==
QEvent
::
MouseButtonPress
)
&&
index
.
isValid
())
{
QMouseEvent
*
const
mouseEvent
=
static_cast
<
QMouseEvent
*>
(
event
);
if
(
mouseEvent
->
button
()
==
Qt
::
RightButton
)
{
static_cast
<
PlayerListWidget
*>
(
parent
())
->
showContextMenu
(
mouseEvent
->
globalPos
(),
index
);
return
true
;
}
}
return
QStyledItemDelegate
::
editorEvent
(
event
,
model
,
option
,
index
);
}
PlayerListTWI
::
PlayerListTWI
()
:
QTreeWidgetItem
(
Type
)
:
QTreeWidgetItem
(
Type
)
{
}
bool
PlayerListTWI
::
operator
<
(
const
QTreeWidgetItem
&
other
)
const
{
// Sort by spectator/player
if
(
data
(
1
,
Qt
::
UserRole
)
!=
other
.
data
(
1
,
Qt
::
UserRole
))
return
data
(
1
,
Qt
::
UserRole
).
toBool
();
// Sort by player ID
return
data
(
4
,
Qt
::
UserRole
+
1
).
toInt
()
<
other
.
data
(
4
,
Qt
::
UserRole
+
1
).
toInt
();
// Sort by spectator/player
if
(
data
(
1
,
Qt
::
UserRole
)
!=
other
.
data
(
1
,
Qt
::
UserRole
))
return
data
(
1
,
Qt
::
UserRole
).
toBool
();
// Sort by player ID
return
data
(
4
,
Qt
::
UserRole
+
1
).
toInt
()
<
other
.
data
(
4
,
Qt
::
UserRole
+
1
).
toInt
();
}
PlayerListWidget
::
PlayerListWidget
(
TabSupervisor
*
_tabSupervisor
,
AbstractClient
*
_client
,
TabGame
*
_game
,
QWidget
*
parent
)
:
QTreeWidget
(
parent
),
tabSupervisor
(
_tabSupervisor
),
client
(
_client
),
game
(
_game
),
gameStarted
(
false
)
:
QTreeWidget
(
parent
),
tabSupervisor
(
_tabSupervisor
),
client
(
_client
),
game
(
_game
),
gameStarted
(
false
)
{
readyIcon
=
QIcon
(
":/resources/icon_ready_start.svg"
);
notReadyIcon
=
QIcon
(
":/resources/icon_not_ready_start.svg"
);
concededIcon
=
QIcon
(
":/resources/icon_conceded.svg"
);
playerIcon
=
QIcon
(
":/resources/icon_player.svg"
);
spectatorIcon
=
QIcon
(
":/resources/icon_spectator.svg"
);
lockIcon
=
QIcon
(
":/resources/lock.svg"
);
if
(
tabSupervisor
)
{
itemDelegate
=
new
PlayerListItemDelegate
(
this
);
setItemDelegate
(
itemDelegate
);
userContextMenu
=
new
UserContextMenu
(
tabSupervisor
,
this
,
game
);
connect
(
userContextMenu
,
SIGNAL
(
openMessageDialog
(
QString
,
bool
)),
this
,
SIGNAL
(
openMessageDialog
(
QString
,
bool
)));
}
else
userContextMenu
=
0
;
setMinimumHeight
(
60
);
setIconSize
(
QSize
(
20
,
15
));
setColumnCount
(
6
);
setHeaderHidden
(
true
);
setRootIsDecorated
(
false
);
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
retranslateUi
();
readyIcon
=
QIcon
(
":/resources/icon_ready_start.svg"
);
notReadyIcon
=
QIcon
(
":/resources/icon_not_ready_start.svg"
);
concededIcon
=
QIcon
(
":/resources/icon_conceded.svg"
);
playerIcon
=
QIcon
(
":/resources/icon_player.svg"
);
spectatorIcon
=
QIcon
(
":/resources/icon_spectator.svg"
);
lockIcon
=
QIcon
(
":/resources/lock.svg"
);
if
(
tabSupervisor
)
{
itemDelegate
=
new
PlayerListItemDelegate
(
this
);
setItemDelegate
(
itemDelegate
);
userContextMenu
=
new
UserContextMenu
(
tabSupervisor
,
this
,
game
);
connect
(
userContextMenu
,
SIGNAL
(
openMessageDialog
(
QString
,
bool
)),
this
,
SIGNAL
(
openMessageDialog
(
QString
,
bool
)));
}
else
userContextMenu
=
0
;
setMinimumHeight
(
60
);
setIconSize
(
QSize
(
20
,
15
));
setColumnCount
(
6
);
setHeaderHidden
(
true
);
setRootIsDecorated
(
false
);
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
retranslateUi
();
}
void
PlayerListWidget
::
retranslateUi
()
...
...
@@ -82,97 +82,97 @@ void PlayerListWidget::retranslateUi()
void
PlayerListWidget
::
addPlayer
(
const
ServerInfo_PlayerProperties
&
player
)
{
QTreeWidgetItem
*
newPlayer
=
new
PlayerListTWI
;
players
.
insert
(
player
.
player_id
(),
newPlayer
);
updatePlayerProperties
(
player
);
addTopLevelItem
(
newPlayer
);
sortItems
(
1
,
Qt
::
AscendingOrder
);
QTreeWidgetItem
*
newPlayer
=
new
PlayerListTWI
;
players
.
insert
(
player
.
player_id
(),
newPlayer
);
updatePlayerProperties
(
player
);
addTopLevelItem
(
newPlayer
);
sortItems
(
1
,
Qt
::
AscendingOrder
);
}
void
PlayerListWidget
::
updatePlayerProperties
(
const
ServerInfo_PlayerProperties
&
prop
,
int
playerId
)
{
if
(
playerId
==
-
1
)
playerId
=
prop
.
player_id
();
QTreeWidgetItem
*
player
=
players
.
value
(
playerId
,
0
);
if
(
!
player
)
return
;
if
(
prop
.
has_spectator
())
{
player
->
setIcon
(
1
,
prop
.
spectator
()
?
spectatorIcon
:
playerIcon
);
player
->
setData
(
1
,
Qt
::
UserRole
,
!
prop
.
spectator
());
}
if
(
prop
.
has_conceded
())
player
->
setData
(
2
,
Qt
::
UserRole
,
prop
.
conceded
());
if
(
prop
.
has_ready_start
())
player
->
setData
(
2
,
Qt
::
UserRole
+
1
,
prop
.
ready_start
());
if
(
prop
.
has_conceded
()
||
prop
.
has_ready_start
())
player
->
setIcon
(
2
,
gameStarted
?
(
prop
.
conceded
()
?
concededIcon
:
QIcon
())
:
(
prop
.
ready_start
()
?
readyIcon
:
notReadyIcon
));
if
(
prop
.
has_user_info
())
{
player
->
setData
(
3
,
Qt
::
UserRole
,
prop
.
user_info
().
user_level
());
player
->
setIcon
(
3
,
QIcon
(
UserLevelPixmapGenerator
::
generatePixmap
(
12
,
UserLevelFlags
(
prop
.
user_info
().
user_level
()))));
player
->
setText
(
4
,
QString
::
fromStdString
(
prop
.
user_info
().
name
()));
const
QString
country
=
QString
::
fromStdString
(
prop
.
user_info
().
country
());
if
(
!
country
.
isEmpty
())
player
->
setIcon
(
4
,
QIcon
(
CountryPixmapGenerator
::
generatePixmap
(
12
,
country
)));
player
->
setData
(
4
,
Qt
::
UserRole
,
QString
::
fromStdString
(
prop
.
user_info
().
name
()));
}
if
(
prop
.
has_player_id
())
player
->
setData
(
4
,
Qt
::
UserRole
+
1
,
prop
.
player_id
());
if
(
prop
.
has_deck_hash
())
player
->
setText
(
5
,
QString
::
fromStdString
(
prop
.
deck_hash
()));
if
(
prop
.
has_sideboard_locked
())
player
->
setIcon
(
5
,
prop
.
sideboard_locked
()
?
lockIcon
:
QIcon
());
if
(
prop
.
has_ping_seconds
())
player
->
setIcon
(
0
,
QIcon
(
PingPixmapGenerator
::
generatePixmap
(
12
,
prop
.
ping_seconds
(),
10
)));
if
(
playerId
==
-
1
)
playerId
=
prop
.
player_id
();
QTreeWidgetItem
*
player
=
players
.
value
(
playerId
,
0
);
if
(
!
player
)
return
;
if
(
prop
.
has_spectator
())
{
player
->
setIcon
(
1
,
prop
.
spectator
()
?
spectatorIcon
:
playerIcon
);
player
->
setData
(
1
,
Qt
::
UserRole
,
!
prop
.
spectator
());
}
if
(
prop
.
has_conceded
())
player
->
setData
(
2
,
Qt
::
UserRole
,
prop
.
conceded
());
if
(
prop
.
has_ready_start
())
player
->
setData
(
2
,
Qt
::
UserRole
+
1
,
prop
.
ready_start
());
if
(
prop
.
has_conceded
()
||
prop
.
has_ready_start
())
player
->
setIcon
(
2
,
gameStarted
?
(
prop
.
conceded
()
?
concededIcon
:
QIcon
())
:
(
prop
.
ready_start
()
?
readyIcon
:
notReadyIcon
));
if
(
prop
.
has_user_info
())
{
player
->
setData
(
3
,
Qt
::
UserRole
,
prop
.
user_info
().
user_level
());
player
->
setIcon
(
3
,
QIcon
(
UserLevelPixmapGenerator
::
generatePixmap
(
12
,
UserLevelFlags
(
prop
.
user_info
().
user_level
()))));
player
->
setText
(
4
,
QString
::
fromStdString
(
prop
.
user_info
().
name
()));
const
QString
country
=
QString
::
fromStdString
(
prop
.
user_info
().
country
());
if
(
!
country
.
isEmpty
())
player
->
setIcon
(
4
,
QIcon
(
CountryPixmapGenerator
::
generatePixmap
(
12
,
country
)));
player
->
setData
(
4
,
Qt
::
UserRole
,
QString
::
fromStdString
(
prop
.
user_info
().
name
()));
}
if
(
prop
.
has_player_id
())
player
->
setData
(
4
,
Qt
::
UserRole
+
1
,
prop
.
player_id
());
if
(
prop
.
has_deck_hash
())
player
->
setText
(
5
,
QString
::
fromStdString
(
prop
.
deck_hash
()));
if
(
prop
.
has_sideboard_locked
())
player
->
setIcon
(
5
,
prop
.
sideboard_locked
()
?
lockIcon
:
QIcon
());
if
(
prop
.
has_ping_seconds
())
player
->
setIcon
(
0
,
QIcon
(
PingPixmapGenerator
::
generatePixmap
(
12
,
prop
.
ping_seconds
(),
10
)));
}
void
PlayerListWidget
::
removePlayer
(
int
playerId
)
{
QTreeWidgetItem
*
player
=
players
.
value
(
playerId
,
0
);
if
(
!
player
)
return
;
players
.
remove
(
playerId
);
delete
takeTopLevelItem
(
indexOfTopLevelItem
(
player
));
QTreeWidgetItem
*
player
=
players
.
value
(
playerId
,
0
);
if
(
!
player
)
return
;
players
.
remove
(
playerId
);
delete
takeTopLevelItem
(
indexOfTopLevelItem
(
player
));
}
void
PlayerListWidget
::
setActivePlayer
(
int
playerId
)
{
QMapIterator
<
int
,
QTreeWidgetItem
*>
i
(
players
);
while
(
i
.
hasNext
())
{
i
.
next
();
QTreeWidgetItem
*
twi
=
i
.
value
();
QColor
c
=
i
.
key
()
==
playerId
?
QColor
(
150
,
255
,
150
)
:
Qt
::
white
;
twi
->
setBackground
(
4
,
c
);
}
QMapIterator
<
int
,
QTreeWidgetItem
*>
i
(
players
);
while
(
i
.
hasNext
())
{
i
.
next
();
QTreeWidgetItem
*
twi
=
i
.
value
();
QColor
c
=
i
.
key
()
==
playerId
?
QColor
(
150
,
255
,
150
)
:
Qt
::
white
;
twi
->
setBackground
(
4
,
c
);
}
}
void
PlayerListWidget
::
setGameStarted
(
bool
_gameStarted
,
bool
resuming
)
{
gameStarted
=
_gameStarted
;
QMapIterator
<
int
,
QTreeWidgetItem
*>
i
(
players
);
while
(
i
.
hasNext
())
{
QTreeWidgetItem
*
twi
=
i
.
next
().
value
();
if
(
gameStarted
)
{
if
(
resuming
)
twi
->
setIcon
(
2
,
twi
->
data
(
2
,
Qt
::
UserRole
).
toBool
()
?
concededIcon
:
QIcon
());
else
{
twi
->
setData
(
2
,
Qt
::
UserRole
,
false
);
twi
->
setIcon
(
2
,
QIcon
());
}
}
else
twi
->
setIcon
(
2
,
notReadyIcon
);
}
gameStarted
=
_gameStarted
;
QMapIterator
<
int
,
QTreeWidgetItem
*>
i
(
players
);
while
(
i
.
hasNext
())
{
QTreeWidgetItem
*
twi
=
i
.
next
().
value
();
if
(
gameStarted
)
{
if
(
resuming
)
twi
->
setIcon
(
2
,
twi
->
data
(
2
,
Qt
::
UserRole
).
toBool
()
?
concededIcon
:
QIcon
());
else
{
twi
->
setData
(
2
,
Qt
::
UserRole
,
false
);
twi
->
setIcon
(
2
,
QIcon
());
}
}
else
twi
->
setIcon
(
2
,
notReadyIcon
);
}
}
void
PlayerListWidget
::
showContextMenu
(
const
QPoint
&
pos
,
const
QModelIndex
&
index
)
{
if
(
!
userContextMenu
)
return
;
const
QString
&
userName
=
index
.
sibling
(
index
.
row
(),
4
).
data
(
Qt
::
UserRole
).
toString
();
int
playerId
=
index
.
sibling
(
index
.
row
(),
4
).
data
(
Qt
::
UserRole
+
1
).
toInt
();
UserLevelFlags
userLevel
(
index
.
sibling
(
index
.
row
(),
3
).
data
(
Qt
::
UserRole
).
toInt
());
userContextMenu
->
showContextMenu
(
pos
,
userName
,
userLevel
,
playerId
);
if
(
!
userContextMenu
)
return
;
const
QString
&
userName
=
index
.
sibling
(
index
.
row
(),
4
).
data
(
Qt
::
UserRole
).
toString
();
int
playerId
=
index
.
sibling
(
index
.
row
(),
4
).
data
(
Qt
::
UserRole
+
1
).
toInt
();
UserLevelFlags
userLevel
(
index
.
sibling
(
index
.
row
(),
3
).
data
(
Qt
::
UserRole
).
toInt
());
userContextMenu
->
showContextMenu
(
pos
,
userName
,
userLevel
,
playerId
);
}
cockatrice/src/playerlistwidget.h
View file @
1bc48a78
...
...
@@ -14,38 +14,38 @@ class UserContextMenu;
class
PlayerListItemDelegate
:
public
QStyledItemDelegate
{
public:
PlayerListItemDelegate
(
QObject
*
const
parent
);
bool
editorEvent
(
QEvent
*
event
,
QAbstractItemModel
*
model
,
const
QStyleOptionViewItem
&
option
,
const
QModelIndex
&
index
);
PlayerListItemDelegate
(
QObject
*
const
parent
);
bool
editorEvent
(
QEvent
*
event
,
QAbstractItemModel
*
model
,
const
QStyleOptionViewItem
&
option
,
const
QModelIndex
&
index
);
};
class
PlayerListTWI
:
public
QTreeWidgetItem
{
public:
PlayerListTWI
();
bool
operator
<
(
const
QTreeWidgetItem
&
other
)
const
;
PlayerListTWI
();
bool
operator
<
(
const
QTreeWidgetItem
&
other
)
const
;
};
class
PlayerListWidget
:
public
QTreeWidget
{
Q_OBJECT
Q_OBJECT
private:
PlayerListItemDelegate
*
itemDelegate
;
QMap
<
int
,
QTreeWidgetItem
*>
players
;
TabSupervisor
*
tabSupervisor
;
AbstractClient
*
client
;
TabGame
*
game
;
UserContextMenu
*
userContextMenu
;
QIcon
readyIcon
,
notReadyIcon
,
concededIcon
,
playerIcon
,
spectatorIcon
,
lockIcon
;
bool
gameStarted
;
PlayerListItemDelegate
*
itemDelegate
;
QMap
<
int
,
QTreeWidgetItem
*>
players
;
TabSupervisor
*
tabSupervisor
;
AbstractClient
*
client
;
TabGame
*
game
;
UserContextMenu
*
userContextMenu
;
QIcon
readyIcon
,
notReadyIcon
,
concededIcon
,
playerIcon
,
spectatorIcon
,
lockIcon
;
bool
gameStarted
;
signals:
void
openMessageDialog
(
const
QString
&
userName
,
bool
focus
);
void
openMessageDialog
(
const
QString
&
userName
,
bool
focus
);
public:
PlayerListWidget
(
TabSupervisor
*
_tabSupervisor
,
AbstractClient
*
_client
,
TabGame
*
_game
,
QWidget
*
parent
=
0
);
void
retranslateUi
();
void
addPlayer
(
const
ServerInfo_PlayerProperties
&
player
);
void
removePlayer
(
int
playerId
);
void
setActivePlayer
(
int
playerId
);
void
updatePlayerProperties
(
const
ServerInfo_PlayerProperties
&
prop
,
int
playerId
=
-
1
);
void
setGameStarted
(
bool
_gameStarted
,
bool
resuming
);
void
showContextMenu
(
const
QPoint
&
pos
,
const
QModelIndex
&
index
);
PlayerListWidget
(
TabSupervisor
*
_tabSupervisor
,
AbstractClient
*
_client
,
TabGame
*
_game
,
QWidget
*
parent
=
0
);
void
retranslateUi
();
void
addPlayer
(
const
ServerInfo_PlayerProperties
&
player
);
void
removePlayer
(
int
playerId
);
void
setActivePlayer
(
int
playerId
);
void
updatePlayerProperties
(
const
ServerInfo_PlayerProperties
&
prop
,
int
playerId
=
-
1
);
void
setGameStarted
(
bool
_gameStarted
,
bool
resuming
);
void
showContextMenu
(
const
QPoint
&
pos
,
const
QModelIndex
&
index
);
};
#endif
cockatrice/src/playertarget.cpp
View file @
1bc48a78
...
...
@@ -8,149 +8,149 @@
#include
<math.h>
PlayerCounter
::
PlayerCounter
(
Player
*
_player
,
int
_id
,
const
QString
&
_name
,
int
_value
,
QGraphicsItem
*
parent
)
:
AbstractCounter
(
_player
,
_id
,
_name
,
false
,
_value
,
parent
)
:
AbstractCounter
(
_player
,
_id
,
_name
,
false
,
_value
,
parent
)
{
}
QRectF
PlayerCounter
::
boundingRect
()
const
{
return
QRectF
(
0
,
0
,
50
,
30
);
return
QRectF
(
0
,
0
,
50
,
30
);
}
void
PlayerCounter
::
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
/*option*/
,
QWidget
*
/*widget*/
)
{
const
int
radius
=
8
;
const
qreal
border
=
1
;
QPainterPath
path
(
QPointF
(
50
-
border
/
2
,
border
/
2
));
path
.
lineTo
(
radius
,
border
/
2
);
path
.
arcTo
(
border
/
2
,
border
/
2
,
2
*
radius
,
2
*
radius
,
90
,
90
);
path
.
lineTo
(
border
/
2
,
30
-
border
/
2
);
path
.
lineTo
(
50
-
border
/
2
,
30
-
border
/
2
);
path
.
closeSubpath
();
QPen
pen
(
QColor
(
100
,
100
,
100
));
pen
.
setWidth
(
border
);
painter
->
setPen
(
pen
);
painter
->
setBrush
(
hovered
?
QColor
(
50
,
50
,
50
,
160
)
:
QColor
(
0
,
0
,
0
,
160
));
painter
->
drawPath
(
path
);
const
int
radius
=
8
;
const
qreal
border
=
1
;
QPainterPath
path
(
QPointF
(
50
-
border
/
2
,
border
/
2
));
path
.
lineTo
(
radius
,
border
/
2
);
path
.
arcTo
(
border
/
2
,
border
/
2
,
2
*
radius
,
2
*
radius
,
90
,
90
);
path
.
lineTo
(
border
/
2
,
30
-
border
/
2
);
path
.
lineTo
(
50
-
border
/
2
,
30
-
border
/
2
);
path
.
closeSubpath
();
QPen
pen
(
QColor
(
100
,
100
,
100
));
pen
.
setWidth
(
border
);
painter
->
setPen
(
pen
);
painter
->
setBrush
(
hovered
?
QColor
(
50
,
50
,
50
,
160
)
:
QColor
(
0
,
0
,
0
,
160
));
painter
->
drawPath
(
path
);
QRectF
translatedRect
=
painter
->
combinedTransform
().
mapRect
(
boundingRect
());
QSize
translatedSize
=
translatedRect
.
size
().
toSize
();
painter
->
resetTransform
();
QFont
font
(
"Serif"
);
font
.
setWeight
(
QFont
::
Bold
);
font
.
setPixelSize
(
qMax
((
int
)
round
(
translatedSize
.
height
()
/
1.3
),
9
));
painter
->
setFont
(
font
);
painter
->
setPen
(
Qt
::
white
);
painter
->
drawText
(
translatedRect
,
Qt
::
AlignCenter
,
QString
::
number
(
value
));
QRectF
translatedRect
=
painter
->
combinedTransform
().
mapRect
(
boundingRect
());
QSize
translatedSize
=
translatedRect
.
size
().
toSize
();
painter
->
resetTransform
();
QFont
font
(
"Serif"
);
font
.
setWeight
(
QFont
::
Bold
);
font
.
setPixelSize
(
qMax
((
int
)
round
(
translatedSize
.
height
()
/
1.3
),
9
));
painter
->
setFont
(
font
);
painter
->
setPen
(
Qt
::
white
);
painter
->
drawText
(
translatedRect
,
Qt
::
AlignCenter
,
QString
::
number
(
value
));
}
PlayerTarget
::
PlayerTarget
(
Player
*
_owner
,
QGraphicsItem
*
parentItem
)
:
ArrowTarget
(
_owner
,
parentItem
),
playerCounter
(
0
)
:
ArrowTarget
(
_owner
,
parentItem
),
playerCounter
(
0
)
{
setCacheMode
(
DeviceCoordinateCache
);
const
std
::
string
&
bmp
=
_owner
->
getUserInfo
()
->
avatar_bmp
();
if
(
!
fullPixmap
.
loadFromData
((
const
uchar
*
)
bmp
.
data
(),
bmp
.
size
()))
fullPixmap
=
QPixmap
();
setCacheMode
(
DeviceCoordinateCache
);
const
std
::
string
&
bmp
=
_owner
->
getUserInfo
()
->
avatar_bmp
();
if
(
!
fullPixmap
.
loadFromData
((
const
uchar
*
)
bmp
.
data
(),
bmp
.
size
()))
fullPixmap
=
QPixmap
();
}
PlayerTarget
::~
PlayerTarget
()
{
// Explicit deletion is necessary in spite of parent/child relationship
// as we need this object to be alive to receive the destroyed() signal.
delete
playerCounter
;
// Explicit deletion is necessary in spite of parent/child relationship
// as we need this object to be alive to receive the destroyed() signal.
delete
playerCounter
;
}
QRectF
PlayerTarget
::
boundingRect
()
const
{
return
QRectF
(
0
,
0
,
160
,
64
);
return
QRectF
(
0
,
0
,
160
,
64
);
}
void
PlayerTarget
::
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
/*option*/
,
QWidget
*
/*widget*/
)
{
const
ServerInfo_User
*
const
info
=
owner
->
getUserInfo
();
const
ServerInfo_User
*
const
info
=
owner
->
getUserInfo
();
const
qreal
border
=
2
;
const
qreal
border
=
2
;
QRectF
avatarBoundingRect
=
boundingRect
().
adjusted
(
border
,
border
,
-
border
,
-
border
);
QRectF
translatedRect
=
painter
->
combinedTransform
().
mapRect
(
avatarBoundingRect
);
QSize
translatedSize
=
translatedRect
.
size
().
toSize
();
QPixmap
cachedPixmap
;
const
QString
cacheKey
=
"avatar"
+
QString
::
number
(
translatedSize
.
width
())
+
"_"
+
QString
::
number
(
info
->
user_level
())
+
"_"
+
QString
::
number
(
fullPixmap
.
cacheKey
());
QRectF
avatarBoundingRect
=
boundingRect
().
adjusted
(
border
,
border
,
-
border
,
-
border
);
QRectF
translatedRect
=
painter
->
combinedTransform
().
mapRect
(
avatarBoundingRect
);
QSize
translatedSize
=
translatedRect
.
size
().
toSize
();
QPixmap
cachedPixmap
;
const
QString
cacheKey
=
"avatar"
+
QString
::
number
(
translatedSize
.
width
())
+
"_"
+
QString
::
number
(
info
->
user_level
())
+
"_"
+
QString
::
number
(
fullPixmap
.
cacheKey
());
#if QT_VERSION >= 0x040600
if
(
!
QPixmapCache
::
find
(
cacheKey
,
&
cachedPixmap
))
{
if
(
!
QPixmapCache
::
find
(
cacheKey
,
&
cachedPixmap
))
{
#else
if
(
!
QPixmapCache
::
find
(
cacheKey
,
cachedPixmap
))
{
if
(
!
QPixmapCache
::
find
(
cacheKey
,
cachedPixmap
))
{
#endif
cachedPixmap
=
QPixmap
(
translatedSize
.
width
(),
translatedSize
.
height
());
QPainter
tempPainter
(
&
cachedPixmap
);
QRadialGradient
grad
(
translatedRect
.
center
(),
sqrt
(
translatedSize
.
width
()
*
translatedSize
.
width
()
+
translatedSize
.
height
()
*
translatedSize
.
height
())
/
2
);
grad
.
setColorAt
(
1
,
Qt
::
black
);
grad
.
setColorAt
(
0
,
QColor
(
180
,
180
,
180
));
tempPainter
.
fillRect
(
QRectF
(
0
,
0
,
translatedSize
.
width
(),
translatedSize
.
height
()),
grad
);
QPixmap
tempPixmap
;
if
(
fullPixmap
.
isNull
())
tempPixmap
=
UserLevelPixmapGenerator
::
generatePixmap
(
translatedSize
.
height
(),
UserLevelFlags
(
info
->
user_level
()));
else
tempPixmap
=
fullPixmap
.
scaled
(
translatedSize
,
Qt
::
KeepAspectRatio
,
Qt
::
SmoothTransformation
);
tempPainter
.
drawPixmap
((
translatedSize
.
width
()
-
tempPixmap
.
width
())
/
2
,
(
translatedSize
.
height
()
-
tempPixmap
.
height
())
/
2
,
tempPixmap
);
QPixmapCache
::
insert
(
cacheKey
,
cachedPixmap
);
}
painter
->
save
();
painter
->
resetTransform
();
painter
->
translate
((
translatedSize
.
width
()
-
cachedPixmap
.
width
())
/
2.0
,
0
);
painter
->
drawPixmap
(
translatedRect
,
cachedPixmap
,
cachedPixmap
.
rect
());
painter
->
restore
();
cachedPixmap
=
QPixmap
(
translatedSize
.
width
(),
translatedSize
.
height
());
QPainter
tempPainter
(
&
cachedPixmap
);
QRadialGradient
grad
(
translatedRect
.
center
(),
sqrt
(
translatedSize
.
width
()
*
translatedSize
.
width
()
+
translatedSize
.
height
()
*
translatedSize
.
height
())
/
2
);
grad
.
setColorAt
(
1
,
Qt
::
black
);
grad
.
setColorAt
(
0
,
QColor
(
180
,
180
,
180
));
tempPainter
.
fillRect
(
QRectF
(
0
,
0
,
translatedSize
.
width
(),
translatedSize
.
height
()),
grad
);
QPixmap
tempPixmap
;
if
(
fullPixmap
.
isNull
())
tempPixmap
=
UserLevelPixmapGenerator
::
generatePixmap
(
translatedSize
.
height
(),
UserLevelFlags
(
info
->
user_level
()));
else
tempPixmap
=
fullPixmap
.
scaled
(
translatedSize
,
Qt
::
KeepAspectRatio
,
Qt
::
SmoothTransformation
);
tempPainter
.
drawPixmap
((
translatedSize
.
width
()
-
tempPixmap
.
width
())
/
2
,
(
translatedSize
.
height
()
-
tempPixmap
.
height
())
/
2
,
tempPixmap
);
QPixmapCache
::
insert
(
cacheKey
,
cachedPixmap
);
}
painter
->
save
();
painter
->
resetTransform
();
painter
->
translate
((
translatedSize
.
width
()
-
cachedPixmap
.
width
())
/
2.0
,
0
);
painter
->
drawPixmap
(
translatedRect
,
cachedPixmap
,
cachedPixmap
.
rect
());
painter
->
restore
();
QRectF
nameRect
=
QRectF
(
0
,
boundingRect
().
height
()
-
20
,
110
,
20
);
painter
->
fillRect
(
nameRect
,
QColor
(
0
,
0
,
0
,
160
));
QRectF
translatedNameRect
=
painter
->
combinedTransform
().
mapRect
(
nameRect
);
painter
->
save
();
painter
->
resetTransform
();
QString
name
=
QString
::
fromStdString
(
info
->
name
());
if
(
name
.
size
()
>
13
)
name
=
name
.
mid
(
0
,
10
)
+
"..."
;
QFont
font
;
font
.
setPixelSize
(
qMax
((
int
)
round
(
translatedNameRect
.
height
()
/
1.5
),
9
));
painter
->
setFont
(
font
);
painter
->
setPen
(
Qt
::
white
);
painter
->
drawText
(
translatedNameRect
,
Qt
::
AlignVCenter
|
Qt
::
AlignLeft
,
" "
+
name
);
painter
->
restore
();
QRectF
nameRect
=
QRectF
(
0
,
boundingRect
().
height
()
-
20
,
110
,
20
);
painter
->
fillRect
(
nameRect
,
QColor
(
0
,
0
,
0
,
160
));
QRectF
translatedNameRect
=
painter
->
combinedTransform
().
mapRect
(
nameRect
);
painter
->
save
();
painter
->
resetTransform
();
QString
name
=
QString
::
fromStdString
(
info
->
name
());
if
(
name
.
size
()
>
13
)
name
=
name
.
mid
(
0
,
10
)
+
"..."
;
QFont
font
;
font
.
setPixelSize
(
qMax
((
int
)
round
(
translatedNameRect
.
height
()
/
1.5
),
9
));
painter
->
setFont
(
font
);
painter
->
setPen
(
Qt
::
white
);
painter
->
drawText
(
translatedNameRect
,
Qt
::
AlignVCenter
|
Qt
::
AlignLeft
,
" "
+
name
);
painter
->
restore
();
QPen
pen
(
QColor
(
100
,
100
,
100
));
pen
.
setWidth
(
border
);
pen
.
setJoinStyle
(
Qt
::
RoundJoin
);
painter
->
setPen
(
pen
);
painter
->
drawRect
(
boundingRect
().
adjusted
(
border
/
2
,
border
/
2
,
-
border
/
2
,
-
border
/
2
));
if
(
getBeingPointedAt
())
painter
->
fillRect
(
boundingRect
(),
QBrush
(
QColor
(
255
,
0
,
0
,
100
)));
QPen
pen
(
QColor
(
100
,
100
,
100
));
pen
.
setWidth
(
border
);
pen
.
setJoinStyle
(
Qt
::
RoundJoin
);
painter
->
setPen
(
pen
);
painter
->
drawRect
(
boundingRect
().
adjusted
(
border
/
2
,
border
/
2
,
-
border
/
2
,
-
border
/
2
));
if
(
getBeingPointedAt
())
painter
->
fillRect
(
boundingRect
(),
QBrush
(
QColor
(
255
,
0
,
0
,
100
)));
}
AbstractCounter
*
PlayerTarget
::
addCounter
(
int
_counterId
,
const
QString
&
_name
,
int
_value
)
{
if
(
playerCounter
)
{
disconnect
(
playerCounter
,
0
,
this
,
0
);
playerCounter
->
delCounter
();
}
if
(
playerCounter
)
{
disconnect
(
playerCounter
,
0
,
this
,
0
);
playerCounter
->
delCounter
();
}
playerCounter
=
new
PlayerCounter
(
owner
,
_counterId
,
_name
,
_value
,
this
);
playerCounter
->
setPos
(
boundingRect
().
width
()
-
playerCounter
->
boundingRect
().
width
(),
boundingRect
().
height
()
-
playerCounter
->
boundingRect
().
height
());
connect
(
playerCounter
,
SIGNAL
(
destroyed
()),
this
,
SLOT
(
counterDeleted
()));
return
playerCounter
;
playerCounter
=
new
PlayerCounter
(
owner
,
_counterId
,
_name
,
_value
,
this
);
playerCounter
->
setPos
(
boundingRect
().
width
()
-
playerCounter
->
boundingRect
().
width
(),
boundingRect
().
height
()
-
playerCounter
->
boundingRect
().
height
());
connect
(
playerCounter
,
SIGNAL
(
destroyed
()),
this
,
SLOT
(
counterDeleted
()));
return
playerCounter
;
}
void
PlayerTarget
::
counterDeleted
()
{
playerCounter
=
0
;
playerCounter
=
0
;
}
cockatrice/src/playertarget.h
View file @
1bc48a78
...
...
@@ -9,30 +9,30 @@
class
Player
;
class
PlayerCounter
:
public
AbstractCounter
{
Q_OBJECT
Q_OBJECT
public:
PlayerCounter
(
Player
*
_player
,
int
_id
,
const
QString
&
_name
,
int
_value
,
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
PlayerCounter
(
Player
*
_player
,
int
_id
,
const
QString
&
_name
,
int
_value
,
QGraphicsItem
*
parent
=
0
);
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
};
class
PlayerTarget
:
public
ArrowTarget
{
Q_OBJECT
Q_OBJECT
private:
QPixmap
fullPixmap
;
PlayerCounter
*
playerCounter
;
QPixmap
fullPixmap
;
PlayerCounter
*
playerCounter
;
public
slots
:
void
counterDeleted
();
void
counterDeleted
();
public:
enum
{
Type
=
typePlayerTarget
};
int
type
()
const
{
return
Type
;
}
PlayerTarget
(
Player
*
_player
=
0
,
QGraphicsItem
*
parentItem
=
0
);
~
PlayerTarget
();
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
AbstractCounter
*
addCounter
(
int
_counterId
,
const
QString
&
_name
,
int
_value
);
enum
{
Type
=
typePlayerTarget
};
int
type
()
const
{
return
Type
;
}
PlayerTarget
(
Player
*
_player
=
0
,
QGraphicsItem
*
parentItem
=
0
);
~
PlayerTarget
();
QRectF
boundingRect
()
const
;
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
);
AbstractCounter
*
addCounter
(
int
_counterId
,
const
QString
&
_name
,
int
_value
);
};
#endif
cockatrice/src/qt-json/json.cpp
View file @
1bc48a78
...
...
@@ -131,30 +131,30 @@ QByteArray Json::serialize(const QVariant &data, bool &success)
str
=
"[ "
+
join
(
values
,
", "
)
+
" ]"
;
}
else
if
(
data
.
type
()
==
QVariant
::
Hash
)
// variant is a hash?
{
const
QVariantHash
vhash
=
data
.
toHash
();
QHashIterator
<
QString
,
QVariant
>
it
(
vhash
);
str
=
"{ "
;
QList
<
QByteArray
>
pairs
;
while
(
it
.
hasNext
())
{
it
.
next
();
QByteArray
serializedValue
=
serialize
(
it
.
value
());
if
(
serializedValue
.
isNull
())
{
success
=
false
;
break
;
}
pairs
<<
sanitizeString
(
it
.
key
()).
toUtf8
()
+
" : "
+
serializedValue
;
}
str
+=
join
(
pairs
,
", "
);
str
+=
" }"
;
}
else
if
(
data
.
type
()
==
QVariant
::
Hash
)
// variant is a hash?
{
const
QVariantHash
vhash
=
data
.
toHash
();
QHashIterator
<
QString
,
QVariant
>
it
(
vhash
);
str
=
"{ "
;
QList
<
QByteArray
>
pairs
;
while
(
it
.
hasNext
())
{
it
.
next
();
QByteArray
serializedValue
=
serialize
(
it
.
value
());
if
(
serializedValue
.
isNull
())
{
success
=
false
;
break
;
}
pairs
<<
sanitizeString
(
it
.
key
()).
toUtf8
()
+
" : "
+
serializedValue
;
}
str
+=
join
(
pairs
,
", "
);
str
+=
" }"
;
}
else
if
(
data
.
type
()
==
QVariant
::
Map
)
// variant is a map?
{
const
QVariantMap
vmap
=
data
.
toMap
();
...
...
cockatrice/src/remoteclient.cpp
View file @
1bc48a78
...
...
@@ -12,220 +12,220 @@
static
const
unsigned
int
protocolVersion
=
14
;
RemoteClient
::
RemoteClient
(
QObject
*
parent
)
:
AbstractClient
(
parent
),
timeRunning
(
0
),
lastDataReceived
(
0
),
messageInProgress
(
false
),
handshakeStarted
(
false
),
messageLength
(
0
)
:
AbstractClient
(
parent
),
timeRunning
(
0
),
lastDataReceived
(
0
),
messageInProgress
(
false
),
handshakeStarted
(
false
),
messageLength
(
0
)
{
timer
=
new
QTimer
(
this
);
timer
->
setInterval
(
1000
);
connect
(
timer
,
SIGNAL
(
timeout
()),
this
,
SLOT
(
ping
()));
socket
=
new
QTcpSocket
(
this
);
socket
->
setSocketOption
(
QAbstractSocket
::
LowDelayOption
,
1
);
connect
(
socket
,
SIGNAL
(
connected
()),
this
,
SLOT
(
slotConnected
()));
connect
(
socket
,
SIGNAL
(
readyRead
()),
this
,
SLOT
(
readData
()));
connect
(
socket
,
SIGNAL
(
error
(
QAbstractSocket
::
SocketError
)),
this
,
SLOT
(
slotSocketError
(
QAbstractSocket
::
SocketError
)));
connect
(
this
,
SIGNAL
(
serverIdentificationEventReceived
(
const
Event_ServerIdentification
&
)),
this
,
SLOT
(
processServerIdentificationEvent
(
const
Event_ServerIdentification
&
)));
connect
(
this
,
SIGNAL
(
connectionClosedEventReceived
(
Event_ConnectionClosed
)),
this
,
SLOT
(
processConnectionClosedEvent
(
Event_ConnectionClosed
)));
connect
(
this
,
SIGNAL
(
sigConnectToServer
(
QString
,
unsigned
int
,
QString
,
QString
)),
this
,
SLOT
(
doConnectToServer
(
QString
,
unsigned
int
,
QString
,
QString
)));
connect
(
this
,
SIGNAL
(
sigDisconnectFromServer
()),
this
,
SLOT
(
doDisconnectFromServer
()));
timer
=
new
QTimer
(
this
);
timer
->
setInterval
(
1000
);
connect
(
timer
,
SIGNAL
(
timeout
()),
this
,
SLOT
(
ping
()));
socket
=
new
QTcpSocket
(
this
);
socket
->
setSocketOption
(
QAbstractSocket
::
LowDelayOption
,
1
);
connect
(
socket
,
SIGNAL
(
connected
()),
this
,
SLOT
(
slotConnected
()));
connect
(
socket
,
SIGNAL
(
readyRead
()),
this
,
SLOT
(
readData
()));
connect
(
socket
,
SIGNAL
(
error
(
QAbstractSocket
::
SocketError
)),
this
,
SLOT
(
slotSocketError
(
QAbstractSocket
::
SocketError
)));
connect
(
this
,
SIGNAL
(
serverIdentificationEventReceived
(
const
Event_ServerIdentification
&
)),
this
,
SLOT
(
processServerIdentificationEvent
(
const
Event_ServerIdentification
&
)));
connect
(
this
,
SIGNAL
(
connectionClosedEventReceived
(
Event_ConnectionClosed
)),
this
,
SLOT
(
processConnectionClosedEvent
(
Event_ConnectionClosed
)));
connect
(
this
,
SIGNAL
(
sigConnectToServer
(
QString
,
unsigned
int
,
QString
,
QString
)),
this
,
SLOT
(
doConnectToServer
(
QString
,
unsigned
int
,
QString
,
QString
)));
connect
(
this
,
SIGNAL
(
sigDisconnectFromServer
()),
this
,
SLOT
(
doDisconnectFromServer
()));
}
RemoteClient
::~
RemoteClient
()
{
doDisconnectFromServer
();
thread
()
->
quit
();
doDisconnectFromServer
();
thread
()
->
quit
();
}
void
RemoteClient
::
slotSocketError
(
QAbstractSocket
::
SocketError
/*error*/
)
{
QString
errorString
=
socket
->
errorString
();
doDisconnectFromServer
();
emit
socketError
(
errorString
);
QString
errorString
=
socket
->
errorString
();
doDisconnectFromServer
();
emit
socketError
(
errorString
);
}
void
RemoteClient
::
slotConnected
()
{
timeRunning
=
lastDataReceived
=
0
;
timer
->
start
();
// dirty hack to be compatible with v14 server
sendCommandContainer
(
CommandContainer
());
getNewCmdId
();
// end of hack
setStatus
(
StatusAwaitingWelcome
);
timeRunning
=
lastDataReceived
=
0
;
timer
->
start
();
// dirty hack to be compatible with v14 server
sendCommandContainer
(
CommandContainer
());
getNewCmdId
();
// end of hack
setStatus
(
StatusAwaitingWelcome
);
}
void
RemoteClient
::
processServerIdentificationEvent
(
const
Event_ServerIdentification
&
event
)
{
if
(
event
.
protocol_version
()
!=
protocolVersion
)
{
emit
protocolVersionMismatch
(
protocolVersion
,
event
.
protocol_version
());
setStatus
(
StatusDisconnecting
);
return
;
}
setStatus
(
StatusLoggingIn
);
Command_Login
cmdLogin
;
cmdLogin
.
set_user_name
(
userName
.
toStdString
());
cmdLogin
.
set_password
(
password
.
toStdString
());
PendingCommand
*
pend
=
prepareSessionCommand
(
cmdLogin
);
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
loginResponse
(
Response
)));
sendCommand
(
pend
);
if
(
event
.
protocol_version
()
!=
protocolVersion
)
{
emit
protocolVersionMismatch
(
protocolVersion
,
event
.
protocol_version
());
setStatus
(
StatusDisconnecting
);
return
;
}
setStatus
(
StatusLoggingIn
);
Command_Login
cmdLogin
;
cmdLogin
.
set_user_name
(
userName
.
toStdString
());
cmdLogin
.
set_password
(
password
.
toStdString
());
PendingCommand
*
pend
=
prepareSessionCommand
(
cmdLogin
);
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
loginResponse
(
Response
)));
sendCommand
(
pend
);
}
void
RemoteClient
::
processConnectionClosedEvent
(
const
Event_ConnectionClosed
&
/*event*/
)
{
doDisconnectFromServer
();
doDisconnectFromServer
();
}
void
RemoteClient
::
loginResponse
(
const
Response
&
response
)
{
const
Response_Login
&
resp
=
response
.
GetExtension
(
Response_Login
::
ext
);
if
(
response
.
response_code
()
==
Response
::
RespOk
)
{
setStatus
(
StatusLoggedIn
);
emit
userInfoChanged
(
resp
.
user_info
());
QList
<
ServerInfo_User
>
buddyList
;
for
(
int
i
=
resp
.
buddy_list_size
()
-
1
;
i
>=
0
;
--
i
)
buddyList
.
append
(
resp
.
buddy_list
(
i
));
emit
buddyListReceived
(
buddyList
);
QList
<
ServerInfo_User
>
ignoreList
;
for
(
int
i
=
resp
.
ignore_list_size
()
-
1
;
i
>=
0
;
--
i
)
ignoreList
.
append
(
resp
.
ignore_list
(
i
));
emit
ignoreListReceived
(
ignoreList
);
}
else
{
emit
loginError
(
response
.
response_code
(),
QString
::
fromStdString
(
resp
.
denied_reason_str
()),
resp
.
denied_end_time
());
setStatus
(
StatusDisconnecting
);
}
const
Response_Login
&
resp
=
response
.
GetExtension
(
Response_Login
::
ext
);
if
(
response
.
response_code
()
==
Response
::
RespOk
)
{
setStatus
(
StatusLoggedIn
);
emit
userInfoChanged
(
resp
.
user_info
());
QList
<
ServerInfo_User
>
buddyList
;
for
(
int
i
=
resp
.
buddy_list_size
()
-
1
;
i
>=
0
;
--
i
)
buddyList
.
append
(
resp
.
buddy_list
(
i
));
emit
buddyListReceived
(
buddyList
);
QList
<
ServerInfo_User
>
ignoreList
;
for
(
int
i
=
resp
.
ignore_list_size
()
-
1
;
i
>=
0
;
--
i
)
ignoreList
.
append
(
resp
.
ignore_list
(
i
));
emit
ignoreListReceived
(
ignoreList
);
}
else
{
emit
loginError
(
response
.
response_code
(),
QString
::
fromStdString
(
resp
.
denied_reason_str
()),
resp
.
denied_end_time
());
setStatus
(
StatusDisconnecting
);
}
}
void
RemoteClient
::
readData
()
{
lastDataReceived
=
timeRunning
;
QByteArray
data
=
socket
->
readAll
();
inputBuffer
.
append
(
data
);
do
{
if
(
!
messageInProgress
)
{
if
(
inputBuffer
.
size
()
>=
4
)
{
// dirty hack to be compatible with v14 server that sends 60 bytes of garbage at the beginning
if
(
!
handshakeStarted
)
{
handshakeStarted
=
true
;
if
(
inputBuffer
.
startsWith
(
"<?xm"
))
{
messageInProgress
=
true
;
messageLength
=
60
;
}
}
else
{
// end of hack
messageLength
=
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
0
])
<<
24
)
+
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
1
])
<<
16
)
+
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
2
])
<<
8
)
+
((
quint32
)
(
unsigned
char
)
inputBuffer
[
3
]);
inputBuffer
.
remove
(
0
,
4
);
messageInProgress
=
true
;
}
}
else
return
;
}
if
(
inputBuffer
.
size
()
<
messageLength
)
return
;
ServerMessage
newServerMessage
;
newServerMessage
.
ParseFromArray
(
inputBuffer
.
data
(),
messageLength
);
lastDataReceived
=
timeRunning
;
QByteArray
data
=
socket
->
readAll
();
inputBuffer
.
append
(
data
);
do
{
if
(
!
messageInProgress
)
{
if
(
inputBuffer
.
size
()
>=
4
)
{
// dirty hack to be compatible with v14 server that sends 60 bytes of garbage at the beginning
if
(
!
handshakeStarted
)
{
handshakeStarted
=
true
;
if
(
inputBuffer
.
startsWith
(
"<?xm"
))
{
messageInProgress
=
true
;
messageLength
=
60
;
}
}
else
{
// end of hack
messageLength
=
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
0
])
<<
24
)
+
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
1
])
<<
16
)
+
(((
quint32
)
(
unsigned
char
)
inputBuffer
[
2
])
<<
8
)
+
((
quint32
)
(
unsigned
char
)
inputBuffer
[
3
]);
inputBuffer
.
remove
(
0
,
4
);
messageInProgress
=
true
;
}
}
else
return
;
}
if
(
inputBuffer
.
size
()
<
messageLength
)
return
;
ServerMessage
newServerMessage
;
newServerMessage
.
ParseFromArray
(
inputBuffer
.
data
(),
messageLength
);
#ifdef QT_DEBUG
qDebug
()
<<
"IN"
<<
messageLength
<<
QString
::
fromStdString
(
newServerMessage
.
ShortDebugString
());
qDebug
()
<<
"IN"
<<
messageLength
<<
QString
::
fromStdString
(
newServerMessage
.
ShortDebugString
());
#endif
inputBuffer
.
remove
(
0
,
messageLength
);
messageInProgress
=
false
;
processProtocolItem
(
newServerMessage
);
if
(
getStatus
()
==
StatusDisconnecting
)
// use thread-safe getter
doDisconnectFromServer
();
}
while
(
!
inputBuffer
.
isEmpty
());
inputBuffer
.
remove
(
0
,
messageLength
);
messageInProgress
=
false
;
processProtocolItem
(
newServerMessage
);
if
(
getStatus
()
==
StatusDisconnecting
)
// use thread-safe getter
doDisconnectFromServer
();
}
while
(
!
inputBuffer
.
isEmpty
());
}
void
RemoteClient
::
sendCommandContainer
(
const
CommandContainer
&
cont
)
{
QByteArray
buf
;
unsigned
int
size
=
cont
.
ByteSize
();
QByteArray
buf
;
unsigned
int
size
=
cont
.
ByteSize
();
#ifdef QT_DEBUG
qDebug
()
<<
"OUT"
<<
size
<<
QString
::
fromStdString
(
cont
.
ShortDebugString
());
qDebug
()
<<
"OUT"
<<
size
<<
QString
::
fromStdString
(
cont
.
ShortDebugString
());
#endif
buf
.
resize
(
size
+
4
);
cont
.
SerializeToArray
(
buf
.
data
()
+
4
,
size
);
buf
.
data
()[
3
]
=
(
unsigned
char
)
size
;
buf
.
data
()[
2
]
=
(
unsigned
char
)
(
size
>>
8
);
buf
.
data
()[
1
]
=
(
unsigned
char
)
(
size
>>
16
);
buf
.
data
()[
0
]
=
(
unsigned
char
)
(
size
>>
24
);
socket
->
write
(
buf
);
buf
.
resize
(
size
+
4
);
cont
.
SerializeToArray
(
buf
.
data
()
+
4
,
size
);
buf
.
data
()[
3
]
=
(
unsigned
char
)
size
;
buf
.
data
()[
2
]
=
(
unsigned
char
)
(
size
>>
8
);
buf
.
data
()[
1
]
=
(
unsigned
char
)
(
size
>>
16
);
buf
.
data
()[
0
]
=
(
unsigned
char
)
(
size
>>
24
);
socket
->
write
(
buf
);
}
void
RemoteClient
::
doConnectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
)
{
doDisconnectFromServer
();
userName
=
_userName
;
password
=
_password
;
socket
->
connectToHost
(
hostname
,
port
);
setStatus
(
StatusConnecting
);
doDisconnectFromServer
();
userName
=
_userName
;
password
=
_password
;
socket
->
connectToHost
(
hostname
,
port
);
setStatus
(
StatusConnecting
);
}
void
RemoteClient
::
doDisconnectFromServer
()
{
timer
->
stop
();
messageInProgress
=
false
;
handshakeStarted
=
false
;
messageLength
=
0
;
QList
<
PendingCommand
*>
pc
=
pendingCommands
.
values
();
for
(
int
i
=
0
;
i
<
pc
.
size
();
i
++
)
{
Response
response
;
response
.
set_response_code
(
Response
::
RespNotConnected
);
response
.
set_cmd_id
(
pc
[
i
]
->
getCommandContainer
().
cmd_id
());
pc
[
i
]
->
processResponse
(
response
);
delete
pc
[
i
];
}
pendingCommands
.
clear
();
setStatus
(
StatusDisconnected
);
socket
->
close
();
timer
->
stop
();
messageInProgress
=
false
;
handshakeStarted
=
false
;
messageLength
=
0
;
QList
<
PendingCommand
*>
pc
=
pendingCommands
.
values
();
for
(
int
i
=
0
;
i
<
pc
.
size
();
i
++
)
{
Response
response
;
response
.
set_response_code
(
Response
::
RespNotConnected
);
response
.
set_cmd_id
(
pc
[
i
]
->
getCommandContainer
().
cmd_id
());
pc
[
i
]
->
processResponse
(
response
);
delete
pc
[
i
];
}
pendingCommands
.
clear
();
setStatus
(
StatusDisconnected
);
socket
->
close
();
}
void
RemoteClient
::
ping
()
{
QMutableMapIterator
<
int
,
PendingCommand
*>
i
(
pendingCommands
);
while
(
i
.
hasNext
())
{
PendingCommand
*
pend
=
i
.
next
().
value
();
if
(
pend
->
tick
()
>
maxTimeout
)
{
i
.
remove
();
pend
->
deleteLater
();
}
}
int
maxTime
=
timeRunning
-
lastDataReceived
;
emit
maxPingTime
(
maxTime
,
maxTimeout
);
if
(
maxTime
>=
maxTimeout
)
{
disconnectFromServer
();
emit
serverTimeout
();
}
else
{
sendCommand
(
prepareSessionCommand
(
Command_Ping
()));
++
timeRunning
;
}
QMutableMapIterator
<
int
,
PendingCommand
*>
i
(
pendingCommands
);
while
(
i
.
hasNext
())
{
PendingCommand
*
pend
=
i
.
next
().
value
();
if
(
pend
->
tick
()
>
maxTimeout
)
{
i
.
remove
();
pend
->
deleteLater
();
}
}
int
maxTime
=
timeRunning
-
lastDataReceived
;
emit
maxPingTime
(
maxTime
,
maxTimeout
);
if
(
maxTime
>=
maxTimeout
)
{
disconnectFromServer
();
emit
serverTimeout
();
}
else
{
sendCommand
(
prepareSessionCommand
(
Command_Ping
()));
++
timeRunning
;
}
}
void
RemoteClient
::
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
)
{
emit
sigConnectToServer
(
hostname
,
port
,
_userName
,
_password
);
emit
sigConnectToServer
(
hostname
,
port
,
_userName
,
_password
);
}
void
RemoteClient
::
disconnectFromServer
()
{
emit
sigDisconnectFromServer
();
emit
sigDisconnectFromServer
();
}
cockatrice/src/remoteclient.h
View file @
1bc48a78
...
...
@@ -7,45 +7,45 @@
class
QTimer
;
class
RemoteClient
:
public
AbstractClient
{
Q_OBJECT
Q_OBJECT
signals:
void
maxPingTime
(
int
seconds
,
int
maxSeconds
);
void
serverTimeout
();
void
loginError
(
Response
::
ResponseCode
resp
,
QString
reasonStr
,
quint32
endTime
);
void
socketError
(
const
QString
&
errorString
);
void
protocolVersionMismatch
(
int
clientVersion
,
int
serverVersion
);
void
protocolError
();
void
sigConnectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
sigDisconnectFromServer
();
void
maxPingTime
(
int
seconds
,
int
maxSeconds
);
void
serverTimeout
();
void
loginError
(
Response
::
ResponseCode
resp
,
QString
reasonStr
,
quint32
endTime
);
void
socketError
(
const
QString
&
errorString
);
void
protocolVersionMismatch
(
int
clientVersion
,
int
serverVersion
);
void
protocolError
();
void
sigConnectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
sigDisconnectFromServer
();
private
slots
:
void
slotConnected
();
void
readData
();
void
slotSocketError
(
QAbstractSocket
::
SocketError
error
);
void
ping
();
void
processServerIdentificationEvent
(
const
Event_ServerIdentification
&
event
);
void
processConnectionClosedEvent
(
const
Event_ConnectionClosed
&
event
);
void
loginResponse
(
const
Response
&
response
);
void
doConnectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
doDisconnectFromServer
();
void
slotConnected
();
void
readData
();
void
slotSocketError
(
QAbstractSocket
::
SocketError
error
);
void
ping
();
void
processServerIdentificationEvent
(
const
Event_ServerIdentification
&
event
);
void
processConnectionClosedEvent
(
const
Event_ConnectionClosed
&
event
);
void
loginResponse
(
const
Response
&
response
);
void
doConnectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
doDisconnectFromServer
();
private:
static
const
int
maxTimeout
=
10
;
int
timeRunning
,
lastDataReceived
;
static
const
int
maxTimeout
=
10
;
int
timeRunning
,
lastDataReceived
;
QByteArray
inputBuffer
;
bool
messageInProgress
;
bool
handshakeStarted
;
int
messageLength
;
QTimer
*
timer
;
QTcpSocket
*
socket
;
protected
slots
:
void
sendCommandContainer
(
const
CommandContainer
&
cont
);
QByteArray
inputBuffer
;
bool
messageInProgress
;
bool
handshakeStarted
;
int
messageLength
;
QTimer
*
timer
;
QTcpSocket
*
socket
;
protected
slots
:
void
sendCommandContainer
(
const
CommandContainer
&
cont
);
public:
RemoteClient
(
QObject
*
parent
=
0
);
~
RemoteClient
();
QString
peerName
()
const
{
return
socket
->
peerName
();
}
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
disconnectFromServer
();
RemoteClient
(
QObject
*
parent
=
0
);
~
RemoteClient
();
QString
peerName
()
const
{
return
socket
->
peerName
();
}
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_userName
,
const
QString
&
_password
);
void
disconnectFromServer
();
};
#endif
cockatrice/src/remotedecklist_treewidget.cpp
View file @
1bc48a78
...
...
@@ -10,324 +10,324 @@
#include
"pb/serverinfo_deckstorage.pb.h"
RemoteDeckList_TreeModel
::
DirectoryNode
::
DirectoryNode
(
const
QString
&
_name
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
_parent
)
:
RemoteDeckList_TreeModel
::
Node
(
_name
,
_parent
)
:
RemoteDeckList_TreeModel
::
Node
(
_name
,
_parent
)
{
}
RemoteDeckList_TreeModel
::
DirectoryNode
::~
DirectoryNode
()
{
clearTree
();
clearTree
();
}
void
RemoteDeckList_TreeModel
::
DirectoryNode
::
clearTree
()
{
for
(
int
i
=
0
;
i
<
size
();
++
i
)
delete
at
(
i
);
clear
();
for
(
int
i
=
0
;
i
<
size
();
++
i
)
delete
at
(
i
);
clear
();
}
QString
RemoteDeckList_TreeModel
::
DirectoryNode
::
getPath
()
const
{
if
(
parent
)
{
QString
parentPath
=
parent
->
getPath
();
if
(
parentPath
.
isEmpty
())
return
name
;
else
return
parentPath
+
"/"
+
name
;
}
else
return
name
;
if
(
parent
)
{
QString
parentPath
=
parent
->
getPath
();
if
(
parentPath
.
isEmpty
())
return
name
;
else
return
parentPath
+
"/"
+
name
;
}
else
return
name
;
}
RemoteDeckList_TreeModel
::
DirectoryNode
*
RemoteDeckList_TreeModel
::
DirectoryNode
::
getNodeByPath
(
QStringList
path
)
{
QString
pathItem
;
if
(
parent
)
{
if
(
path
.
isEmpty
())
return
this
;
pathItem
=
path
.
takeFirst
();
if
(
pathItem
.
isEmpty
()
&&
name
.
isEmpty
())
return
this
;
}
for
(
int
i
=
0
;
i
<
size
();
++
i
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
at
(
i
));
if
(
!
node
)
continue
;
if
(
node
->
getName
()
==
pathItem
)
return
node
->
getNodeByPath
(
path
);
}
return
0
;
QString
pathItem
;
if
(
parent
)
{
if
(
path
.
isEmpty
())
return
this
;
pathItem
=
path
.
takeFirst
();
if
(
pathItem
.
isEmpty
()
&&
name
.
isEmpty
())
return
this
;
}
for
(
int
i
=
0
;
i
<
size
();
++
i
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
at
(
i
));
if
(
!
node
)
continue
;
if
(
node
->
getName
()
==
pathItem
)
return
node
->
getNodeByPath
(
path
);
}
return
0
;
}
RemoteDeckList_TreeModel
::
FileNode
*
RemoteDeckList_TreeModel
::
DirectoryNode
::
getNodeById
(
int
id
)
const
{
for
(
int
i
=
0
;
i
<
size
();
++
i
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
at
(
i
));
if
(
node
)
{
FileNode
*
result
=
node
->
getNodeById
(
id
);
if
(
result
)
return
result
;
}
else
{
FileNode
*
file
=
dynamic_cast
<
FileNode
*>
(
at
(
i
));
if
(
file
->
getId
()
==
id
)
return
file
;
}
}
return
0
;
for
(
int
i
=
0
;
i
<
size
();
++
i
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
at
(
i
));
if
(
node
)
{
FileNode
*
result
=
node
->
getNodeById
(
id
);
if
(
result
)
return
result
;
}
else
{
FileNode
*
file
=
dynamic_cast
<
FileNode
*>
(
at
(
i
));
if
(
file
->
getId
()
==
id
)
return
file
;
}
}
return
0
;
}
RemoteDeckList_TreeModel
::
RemoteDeckList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
)
:
QAbstractItemModel
(
parent
),
client
(
_client
)
:
QAbstractItemModel
(
parent
),
client
(
_client
)
{
QFileIconProvider
fip
;
dirIcon
=
fip
.
icon
(
QFileIconProvider
::
Folder
);
fileIcon
=
fip
.
icon
(
QFileIconProvider
::
File
);
QFileIconProvider
fip
;
dirIcon
=
fip
.
icon
(
QFileIconProvider
::
Folder
);
fileIcon
=
fip
.
icon
(
QFileIconProvider
::
File
);
root
=
new
DirectoryNode
;
refreshTree
();
root
=
new
DirectoryNode
;
refreshTree
();
}
RemoteDeckList_TreeModel
::~
RemoteDeckList_TreeModel
()
{
delete
root
;
delete
root
;
}
int
RemoteDeckList_TreeModel
::
rowCount
(
const
QModelIndex
&
parent
)
const
{
DirectoryNode
*
node
=
getNode
<
DirectoryNode
*>
(
parent
);
if
(
node
)
return
node
->
size
();
else
return
0
;
DirectoryNode
*
node
=
getNode
<
DirectoryNode
*>
(
parent
);
if
(
node
)
return
node
->
size
();
else
return
0
;
}
int
RemoteDeckList_TreeModel
::
columnCount
(
const
QModelIndex
&
/*parent*/
)
const
{
return
3
;
return
3
;
}
QVariant
RemoteDeckList_TreeModel
::
data
(
const
QModelIndex
&
index
,
int
role
)
const
{
if
(
!
index
.
isValid
())
return
QVariant
();
if
(
index
.
column
()
>=
3
)
return
QVariant
();
Node
*
temp
=
static_cast
<
Node
*>
(
index
.
internalPointer
());
FileNode
*
file
=
dynamic_cast
<
FileNode
*>
(
temp
);
if
(
!
file
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
temp
);
switch
(
role
)
{
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
node
->
getName
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
dirIcon
:
QVariant
();
default:
return
QVariant
();
}
}
else
{
switch
(
role
)
{
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
file
->
getName
();
case
1
:
return
file
->
getId
();
case
2
:
return
file
->
getUploadTime
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
fileIcon
:
QVariant
();
case
Qt
::
TextAlignmentRole
:
return
index
.
column
()
==
1
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
}
if
(
!
index
.
isValid
())
return
QVariant
();
if
(
index
.
column
()
>=
3
)
return
QVariant
();
Node
*
temp
=
static_cast
<
Node
*>
(
index
.
internalPointer
());
FileNode
*
file
=
dynamic_cast
<
FileNode
*>
(
temp
);
if
(
!
file
)
{
DirectoryNode
*
node
=
dynamic_cast
<
DirectoryNode
*>
(
temp
);
switch
(
role
)
{
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
node
->
getName
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
dirIcon
:
QVariant
();
default:
return
QVariant
();
}
}
else
{
switch
(
role
)
{
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
file
->
getName
();
case
1
:
return
file
->
getId
();
case
2
:
return
file
->
getUploadTime
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
fileIcon
:
QVariant
();
case
Qt
::
TextAlignmentRole
:
return
index
.
column
()
==
1
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
}
}
QVariant
RemoteDeckList_TreeModel
::
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
)
const
{
if
(
orientation
!=
Qt
::
Horizontal
)
return
QVariant
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
return
section
==
1
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
case
Qt
::
DisplayRole
:
{
switch
(
section
)
{
case
0
:
return
tr
(
"Name"
);
case
1
:
return
tr
(
"ID"
);
case
2
:
return
tr
(
"Upload time"
);
default:
return
QVariant
();
}
}
default:
return
QVariant
();
}
if
(
orientation
!=
Qt
::
Horizontal
)
return
QVariant
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
return
section
==
1
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
case
Qt
::
DisplayRole
:
{
switch
(
section
)
{
case
0
:
return
tr
(
"Name"
);
case
1
:
return
tr
(
"ID"
);
case
2
:
return
tr
(
"Upload time"
);
default:
return
QVariant
();
}
}
default:
return
QVariant
();
}
}
QModelIndex
RemoteDeckList_TreeModel
::
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
)
const
{
if
(
!
hasIndex
(
row
,
column
,
parent
))
return
QModelIndex
();
if
(
!
hasIndex
(
row
,
column
,
parent
))
return
QModelIndex
();
DirectoryNode
*
parentNode
=
getNode
<
DirectoryNode
*>
(
parent
);
if
(
row
>=
parentNode
->
size
())
return
QModelIndex
();
DirectoryNode
*
parentNode
=
getNode
<
DirectoryNode
*>
(
parent
);
if
(
row
>=
parentNode
->
size
())
return
QModelIndex
();
return
createIndex
(
row
,
column
,
parentNode
->
at
(
row
));
return
createIndex
(
row
,
column
,
parentNode
->
at
(
row
));
}
QModelIndex
RemoteDeckList_TreeModel
::
parent
(
const
QModelIndex
&
ind
)
const
{
if
(
!
ind
.
isValid
())
return
QModelIndex
();
if
(
!
ind
.
isValid
())
return
QModelIndex
();
return
nodeToIndex
(
static_cast
<
Node
*>
(
ind
.
internalPointer
())
->
getParent
());
return
nodeToIndex
(
static_cast
<
Node
*>
(
ind
.
internalPointer
())
->
getParent
());
}
Qt
::
ItemFlags
RemoteDeckList_TreeModel
::
flags
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
0
;
if
(
!
index
.
isValid
())
return
0
;
return
Qt
::
ItemIsEnabled
|
Qt
::
ItemIsSelectable
;
return
Qt
::
ItemIsEnabled
|
Qt
::
ItemIsSelectable
;
}
QModelIndex
RemoteDeckList_TreeModel
::
nodeToIndex
(
Node
*
node
)
const
{
if
(
node
==
root
)
return
QModelIndex
();
return
createIndex
(
node
->
getParent
()
->
indexOf
(
node
),
0
,
node
);
if
(
node
==
root
)
return
QModelIndex
();
return
createIndex
(
node
->
getParent
()
->
indexOf
(
node
),
0
,
node
);
}
void
RemoteDeckList_TreeModel
::
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
DirectoryNode
*
parent
)
{
const
ServerInfo_DeckStorage_File
&
fileInfo
=
file
.
file
();
QDateTime
time
;
time
.
setTime_t
(
fileInfo
.
creation_time
());
beginInsertRows
(
nodeToIndex
(
parent
),
parent
->
size
(),
parent
->
size
());
parent
->
append
(
new
FileNode
(
QString
::
fromStdString
(
file
.
name
()),
file
.
id
(),
time
,
parent
));
endInsertRows
();
const
ServerInfo_DeckStorage_File
&
fileInfo
=
file
.
file
();
QDateTime
time
;
time
.
setTime_t
(
fileInfo
.
creation_time
());
beginInsertRows
(
nodeToIndex
(
parent
),
parent
->
size
(),
parent
->
size
());
parent
->
append
(
new
FileNode
(
QString
::
fromStdString
(
file
.
name
()),
file
.
id
(),
time
,
parent
));
endInsertRows
();
}
void
RemoteDeckList_TreeModel
::
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
DirectoryNode
*
parent
)
{
DirectoryNode
*
newItem
=
addNamedFolderToTree
(
QString
::
fromStdString
(
folder
.
name
()),
parent
);
const
ServerInfo_DeckStorage_Folder
&
folderInfo
=
folder
.
folder
();
const
int
folderItemsSize
=
folderInfo
.
items_size
();
for
(
int
i
=
0
;
i
<
folderItemsSize
;
++
i
)
{
const
ServerInfo_DeckStorage_TreeItem
&
subItem
=
folderInfo
.
items
(
i
);
if
(
subItem
.
has_folder
())
addFolderToTree
(
subItem
,
newItem
);
else
addFileToTree
(
subItem
,
newItem
);
}
DirectoryNode
*
newItem
=
addNamedFolderToTree
(
QString
::
fromStdString
(
folder
.
name
()),
parent
);
const
ServerInfo_DeckStorage_Folder
&
folderInfo
=
folder
.
folder
();
const
int
folderItemsSize
=
folderInfo
.
items_size
();
for
(
int
i
=
0
;
i
<
folderItemsSize
;
++
i
)
{
const
ServerInfo_DeckStorage_TreeItem
&
subItem
=
folderInfo
.
items
(
i
);
if
(
subItem
.
has_folder
())
addFolderToTree
(
subItem
,
newItem
);
else
addFileToTree
(
subItem
,
newItem
);
}
}
RemoteDeckList_TreeModel
::
DirectoryNode
*
RemoteDeckList_TreeModel
::
addNamedFolderToTree
(
const
QString
&
name
,
DirectoryNode
*
parent
)
{
DirectoryNode
*
newItem
=
new
DirectoryNode
(
name
,
parent
);
beginInsertRows
(
nodeToIndex
(
parent
),
parent
->
size
(),
parent
->
size
());
parent
->
append
(
newItem
);
endInsertRows
();
return
newItem
;
DirectoryNode
*
newItem
=
new
DirectoryNode
(
name
,
parent
);
beginInsertRows
(
nodeToIndex
(
parent
),
parent
->
size
(),
parent
->
size
());
parent
->
append
(
newItem
);
endInsertRows
();
return
newItem
;
}
void
RemoteDeckList_TreeModel
::
removeNode
(
RemoteDeckList_TreeModel
::
Node
*
node
)
{
int
ind
=
node
->
getParent
()
->
indexOf
(
node
);
beginRemoveRows
(
nodeToIndex
(
node
->
getParent
()),
ind
,
ind
);
node
->
getParent
()
->
removeAt
(
ind
);
endRemoveRows
();
delete
node
;
int
ind
=
node
->
getParent
()
->
indexOf
(
node
);
beginRemoveRows
(
nodeToIndex
(
node
->
getParent
()),
ind
,
ind
);
node
->
getParent
()
->
removeAt
(
ind
);
endRemoveRows
();
delete
node
;
}
void
RemoteDeckList_TreeModel
::
refreshTree
()
{
PendingCommand
*
pend
=
client
->
prepareSessionCommand
(
Command_DeckList
());
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
deckListFinished
(
const
Response
&
)));
client
->
sendCommand
(
pend
);
PendingCommand
*
pend
=
client
->
prepareSessionCommand
(
Command_DeckList
());
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
deckListFinished
(
const
Response
&
)));
client
->
sendCommand
(
pend
);
}
void
RemoteDeckList_TreeModel
::
deckListFinished
(
const
Response
&
r
)
{
const
Response_DeckList
&
resp
=
r
.
GetExtension
(
Response_DeckList
::
ext
);
const
Response_DeckList
&
resp
=
r
.
GetExtension
(
Response_DeckList
::
ext
);
root
->
clearTree
();
reset
();
ServerInfo_DeckStorage_TreeItem
tempRoot
;
tempRoot
.
set_id
(
0
);
tempRoot
.
mutable_folder
()
->
CopyFrom
(
resp
.
root
());
addFolderToTree
(
tempRoot
,
root
);
emit
treeRefreshed
();
root
->
clearTree
();
reset
();
ServerInfo_DeckStorage_TreeItem
tempRoot
;
tempRoot
.
set_id
(
0
);
tempRoot
.
mutable_folder
()
->
CopyFrom
(
resp
.
root
());
addFolderToTree
(
tempRoot
,
root
);
emit
treeRefreshed
();
}
RemoteDeckList_TreeWidget
::
RemoteDeckList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
)
:
QTreeView
(
parent
)
:
QTreeView
(
parent
)
{
treeModel
=
new
RemoteDeckList_TreeModel
(
_client
,
this
);
proxyModel
=
new
QSortFilterProxyModel
(
this
);
proxyModel
->
setSourceModel
(
treeModel
);
proxyModel
->
setDynamicSortFilter
(
true
);
proxyModel
->
setSortCaseSensitivity
(
Qt
::
CaseInsensitive
);
setModel
(
proxyModel
);
connect
(
treeModel
,
SIGNAL
(
treeRefreshed
()),
this
,
SLOT
(
expandAll
()));
treeModel
=
new
RemoteDeckList_TreeModel
(
_client
,
this
);
proxyModel
=
new
QSortFilterProxyModel
(
this
);
proxyModel
->
setSourceModel
(
treeModel
);
proxyModel
->
setDynamicSortFilter
(
true
);
proxyModel
->
setSortCaseSensitivity
(
Qt
::
CaseInsensitive
);
setModel
(
proxyModel
);
connect
(
treeModel
,
SIGNAL
(
treeRefreshed
()),
this
,
SLOT
(
expandAll
()));
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
setUniformRowHeights
(
true
);
setSortingEnabled
(
true
);
proxyModel
->
sort
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setSortIndicator
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
setUniformRowHeights
(
true
);
setSortingEnabled
(
true
);
proxyModel
->
sort
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setSortIndicator
(
0
,
Qt
::
AscendingOrder
);
}
RemoteDeckList_TreeModel
::
Node
*
RemoteDeckList_TreeWidget
::
getNode
(
const
QModelIndex
&
ind
)
const
{
return
treeModel
->
getNode
<
RemoteDeckList_TreeModel
::
Node
*>
(
proxyModel
->
mapToSource
(
ind
));
return
treeModel
->
getNode
<
RemoteDeckList_TreeModel
::
Node
*>
(
proxyModel
->
mapToSource
(
ind
));
}
RemoteDeckList_TreeModel
::
Node
*
RemoteDeckList_TreeWidget
::
getCurrentItem
()
const
{
return
getNode
(
selectionModel
()
->
currentIndex
());
return
getNode
(
selectionModel
()
->
currentIndex
());
}
RemoteDeckList_TreeModel
::
DirectoryNode
*
RemoteDeckList_TreeWidget
::
getNodeByPath
(
const
QString
&
path
)
const
{
return
treeModel
->
getRoot
()
->
getNodeByPath
(
path
.
split
(
"/"
));
return
treeModel
->
getRoot
()
->
getNodeByPath
(
path
.
split
(
"/"
));
}
RemoteDeckList_TreeModel
::
FileNode
*
RemoteDeckList_TreeWidget
::
getNodeById
(
int
id
)
const
{
return
treeModel
->
getRoot
()
->
getNodeById
(
id
);
return
treeModel
->
getRoot
()
->
getNodeById
(
id
);
}
void
RemoteDeckList_TreeWidget
::
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
)
{
treeModel
->
addFileToTree
(
file
,
parent
);
treeModel
->
addFileToTree
(
file
,
parent
);
}
void
RemoteDeckList_TreeWidget
::
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
)
{
treeModel
->
addFolderToTree
(
folder
,
parent
);
treeModel
->
addFolderToTree
(
folder
,
parent
);
}
void
RemoteDeckList_TreeWidget
::
addFolderToTree
(
const
QString
&
name
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
)
{
treeModel
->
addNamedFolderToTree
(
name
,
parent
);
treeModel
->
addNamedFolderToTree
(
name
,
parent
);
}
void
RemoteDeckList_TreeWidget
::
removeNode
(
RemoteDeckList_TreeModel
::
Node
*
node
)
{
treeModel
->
removeNode
(
node
);
treeModel
->
removeNode
(
node
);
}
void
RemoteDeckList_TreeWidget
::
refreshTree
()
{
treeModel
->
refreshTree
();
treeModel
->
refreshTree
();
}
cockatrice/src/remotedecklist_treewidget.h
View file @
1bc48a78
...
...
@@ -11,92 +11,92 @@ class QSortFilterProxyModel;
class
ServerInfo_DeckStorage_TreeItem
;
class
RemoteDeckList_TreeModel
:
public
QAbstractItemModel
{
Q_OBJECT
Q_OBJECT
public:
class
DirectoryNode
;
class
FileNode
;
class
Node
{
protected:
DirectoryNode
*
parent
;
QString
name
;
public:
Node
(
const
QString
&
_name
,
DirectoryNode
*
_parent
=
0
)
:
parent
(
_parent
),
name
(
_name
)
{
}
virtual
~
Node
()
{
};
DirectoryNode
*
getParent
()
const
{
return
parent
;
}
QString
getName
()
const
{
return
name
;
}
};
class
DirectoryNode
:
public
Node
,
public
QList
<
Node
*>
{
public:
DirectoryNode
(
const
QString
&
_name
=
QString
(),
DirectoryNode
*
_parent
=
0
);
~
DirectoryNode
();
void
clearTree
();
QString
getPath
()
const
;
DirectoryNode
*
getNodeByPath
(
QStringList
path
);
FileNode
*
getNodeById
(
int
id
)
const
;
};
class
FileNode
:
public
Node
{
private:
int
id
;
QDateTime
uploadTime
;
public:
FileNode
(
const
QString
&
_name
,
int
_id
,
const
QDateTime
&
_uploadTime
,
DirectoryNode
*
_parent
=
0
)
:
Node
(
_name
,
_parent
),
id
(
_id
),
uploadTime
(
_uploadTime
)
{
}
int
getId
()
const
{
return
id
;
}
QDateTime
getUploadTime
()
const
{
return
uploadTime
;
}
};
template
<
typename
T
>
T
getNode
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
dynamic_cast
<
T
>
(
root
);
return
dynamic_cast
<
T
>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
}
class
DirectoryNode
;
class
FileNode
;
class
Node
{
protected:
DirectoryNode
*
parent
;
QString
name
;
public:
Node
(
const
QString
&
_name
,
DirectoryNode
*
_parent
=
0
)
:
parent
(
_parent
),
name
(
_name
)
{
}
virtual
~
Node
()
{
};
DirectoryNode
*
getParent
()
const
{
return
parent
;
}
QString
getName
()
const
{
return
name
;
}
};
class
DirectoryNode
:
public
Node
,
public
QList
<
Node
*>
{
public:
DirectoryNode
(
const
QString
&
_name
=
QString
(),
DirectoryNode
*
_parent
=
0
);
~
DirectoryNode
();
void
clearTree
();
QString
getPath
()
const
;
DirectoryNode
*
getNodeByPath
(
QStringList
path
);
FileNode
*
getNodeById
(
int
id
)
const
;
};
class
FileNode
:
public
Node
{
private:
int
id
;
QDateTime
uploadTime
;
public:
FileNode
(
const
QString
&
_name
,
int
_id
,
const
QDateTime
&
_uploadTime
,
DirectoryNode
*
_parent
=
0
)
:
Node
(
_name
,
_parent
),
id
(
_id
),
uploadTime
(
_uploadTime
)
{
}
int
getId
()
const
{
return
id
;
}
QDateTime
getUploadTime
()
const
{
return
uploadTime
;
}
};
template
<
typename
T
>
T
getNode
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
dynamic_cast
<
T
>
(
root
);
return
dynamic_cast
<
T
>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
}
private:
AbstractClient
*
client
;
DirectoryNode
*
root
;
QIcon
fileIcon
,
dirIcon
;
QModelIndex
nodeToIndex
(
Node
*
node
)
const
;
AbstractClient
*
client
;
DirectoryNode
*
root
;
QIcon
fileIcon
,
dirIcon
;
QModelIndex
nodeToIndex
(
Node
*
node
)
const
;
signals:
void
treeRefreshed
();
void
treeRefreshed
();
private
slots
:
void
deckListFinished
(
const
Response
&
r
);
void
deckListFinished
(
const
Response
&
r
);
public:
RemoteDeckList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
=
0
);
~
RemoteDeckList_TreeModel
();
int
rowCount
(
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
;
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
QModelIndex
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
QModelIndex
parent
(
const
QModelIndex
&
index
)
const
;
Qt
::
ItemFlags
flags
(
const
QModelIndex
&
index
)
const
;
RemoteDeckList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
=
0
);
~
RemoteDeckList_TreeModel
();
int
rowCount
(
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
;
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
QModelIndex
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
QModelIndex
parent
(
const
QModelIndex
&
index
)
const
;
Qt
::
ItemFlags
flags
(
const
QModelIndex
&
index
)
const
;
DirectoryNode
*
getRoot
()
const
{
return
root
;
}
void
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
DirectoryNode
*
parent
);
DirectoryNode
*
addNamedFolderToTree
(
const
QString
&
name
,
DirectoryNode
*
parent
);
void
removeNode
(
Node
*
node
);
void
refreshTree
();
DirectoryNode
*
getRoot
()
const
{
return
root
;
}
void
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
DirectoryNode
*
parent
);
DirectoryNode
*
addNamedFolderToTree
(
const
QString
&
name
,
DirectoryNode
*
parent
);
void
removeNode
(
Node
*
node
);
void
refreshTree
();
};
class
RemoteDeckList_TreeWidget
:
public
QTreeView
{
private:
RemoteDeckList_TreeModel
*
treeModel
;
QSortFilterProxyModel
*
proxyModel
;
RemoteDeckList_TreeModel
*
treeModel
;
QSortFilterProxyModel
*
proxyModel
;
public:
RemoteDeckList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
=
0
);
RemoteDeckList_TreeModel
::
Node
*
getNode
(
const
QModelIndex
&
ind
)
const
;
RemoteDeckList_TreeModel
::
Node
*
getCurrentItem
()
const
;
RemoteDeckList_TreeModel
::
DirectoryNode
*
getNodeByPath
(
const
QString
&
path
)
const
;
RemoteDeckList_TreeModel
::
FileNode
*
getNodeById
(
int
id
)
const
;
void
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
QString
&
name
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
removeNode
(
RemoteDeckList_TreeModel
::
Node
*
node
);
void
refreshTree
();
RemoteDeckList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
=
0
);
RemoteDeckList_TreeModel
::
Node
*
getNode
(
const
QModelIndex
&
ind
)
const
;
RemoteDeckList_TreeModel
::
Node
*
getCurrentItem
()
const
;
RemoteDeckList_TreeModel
::
DirectoryNode
*
getNodeByPath
(
const
QString
&
path
)
const
;
RemoteDeckList_TreeModel
::
FileNode
*
getNodeById
(
int
id
)
const
;
void
addFileToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
file
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
ServerInfo_DeckStorage_TreeItem
&
folder
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
addFolderToTree
(
const
QString
&
name
,
RemoteDeckList_TreeModel
::
DirectoryNode
*
parent
);
void
removeNode
(
RemoteDeckList_TreeModel
::
Node
*
node
);
void
refreshTree
();
};
#endif
cockatrice/src/remotereplaylist_treewidget.cpp
View file @
1bc48a78
...
...
@@ -12,286 +12,286 @@
const
int
RemoteReplayList_TreeModel
::
numberOfColumns
=
6
;
RemoteReplayList_TreeModel
::
MatchNode
::
MatchNode
(
const
ServerInfo_ReplayMatch
&
_matchInfo
)
:
RemoteReplayList_TreeModel
::
Node
(
QString
::
fromStdString
(
_matchInfo
.
game_name
())),
matchInfo
(
_matchInfo
)
:
RemoteReplayList_TreeModel
::
Node
(
QString
::
fromStdString
(
_matchInfo
.
game_name
())),
matchInfo
(
_matchInfo
)
{
for
(
int
i
=
0
;
i
<
matchInfo
.
replay_list_size
();
++
i
)
append
(
new
ReplayNode
(
matchInfo
.
replay_list
(
i
),
this
));
for
(
int
i
=
0
;
i
<
matchInfo
.
replay_list_size
();
++
i
)
append
(
new
ReplayNode
(
matchInfo
.
replay_list
(
i
),
this
));
}
RemoteReplayList_TreeModel
::
MatchNode
::~
MatchNode
()
{
for
(
int
i
=
0
;
i
<
size
();
++
i
)
delete
at
(
i
);
for
(
int
i
=
0
;
i
<
size
();
++
i
)
delete
at
(
i
);
}
void
RemoteReplayList_TreeModel
::
MatchNode
::
updateMatchInfo
(
const
ServerInfo_ReplayMatch
&
_matchInfo
)
{
matchInfo
.
MergeFrom
(
_matchInfo
);
matchInfo
.
MergeFrom
(
_matchInfo
);
}
RemoteReplayList_TreeModel
::
RemoteReplayList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
)
:
QAbstractItemModel
(
parent
),
client
(
_client
)
:
QAbstractItemModel
(
parent
),
client
(
_client
)
{
QFileIconProvider
fip
;
dirIcon
=
fip
.
icon
(
QFileIconProvider
::
Folder
);
fileIcon
=
fip
.
icon
(
QFileIconProvider
::
File
);
lockIcon
=
QIcon
(
":/resources/lock.svg"
);
QFileIconProvider
fip
;
dirIcon
=
fip
.
icon
(
QFileIconProvider
::
Folder
);
fileIcon
=
fip
.
icon
(
QFileIconProvider
::
File
);
lockIcon
=
QIcon
(
":/resources/lock.svg"
);
refreshTree
();
refreshTree
();
}
RemoteReplayList_TreeModel
::~
RemoteReplayList_TreeModel
()
{
clearTree
();
clearTree
();
}
int
RemoteReplayList_TreeModel
::
rowCount
(
const
QModelIndex
&
parent
)
const
{
if
(
!
parent
.
isValid
())
return
replayMatches
.
size
();
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
parent
.
internalPointer
()));
if
(
matchNode
)
return
matchNode
->
size
();
else
return
0
;
if
(
!
parent
.
isValid
())
return
replayMatches
.
size
();
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
parent
.
internalPointer
()));
if
(
matchNode
)
return
matchNode
->
size
();
else
return
0
;
}
QVariant
RemoteReplayList_TreeModel
::
data
(
const
QModelIndex
&
index
,
int
role
)
const
{
if
(
!
index
.
isValid
())
return
QVariant
();
if
(
index
.
column
()
>=
numberOfColumns
)
return
QVariant
();
ReplayNode
*
replayNode
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
replayNode
)
{
const
ServerInfo_Replay
&
replayInfo
=
replayNode
->
getReplayInfo
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
return
index
.
column
()
==
0
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
replayInfo
.
replay_id
();
case
1
:
return
QString
::
fromStdString
(
replayInfo
.
replay_name
());
case
5
:
return
replayInfo
.
duration
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
fileIcon
:
QVariant
();
}
}
else
{
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
const
ServerInfo_ReplayMatch
&
matchInfo
=
matchNode
->
getMatchInfo
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
switch
(
index
.
column
())
{
case
0
:
case
5
:
return
Qt
::
AlignRight
;
default:
return
Qt
::
AlignLeft
;
}
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
matchInfo
.
game_id
();
case
1
:
return
QString
::
fromStdString
(
matchInfo
.
game_name
());
case
2
:
{
QStringList
playerList
;
for
(
int
i
=
0
;
i
<
matchInfo
.
player_names_size
();
++
i
)
playerList
.
append
(
QString
::
fromStdString
(
matchInfo
.
player_names
(
i
)));
return
playerList
.
join
(
", "
);
}
case
4
:
return
QDateTime
::
fromTime_t
(
matchInfo
.
time_started
());
case
5
:
return
matchInfo
.
length
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
switch
(
index
.
column
())
{
case
0
:
return
dirIcon
;
case
3
:
return
matchInfo
.
do_not_hide
()
?
lockIcon
:
QVariant
();
default:
return
QVariant
();
}
}
}
return
QVariant
();
if
(
!
index
.
isValid
())
return
QVariant
();
if
(
index
.
column
()
>=
numberOfColumns
)
return
QVariant
();
ReplayNode
*
replayNode
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
replayNode
)
{
const
ServerInfo_Replay
&
replayInfo
=
replayNode
->
getReplayInfo
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
return
index
.
column
()
==
0
?
Qt
::
AlignRight
:
Qt
::
AlignLeft
;
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
replayInfo
.
replay_id
();
case
1
:
return
QString
::
fromStdString
(
replayInfo
.
replay_name
());
case
5
:
return
replayInfo
.
duration
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
return
index
.
column
()
==
0
?
fileIcon
:
QVariant
();
}
}
else
{
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
const
ServerInfo_ReplayMatch
&
matchInfo
=
matchNode
->
getMatchInfo
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
switch
(
index
.
column
())
{
case
0
:
case
5
:
return
Qt
::
AlignRight
;
default:
return
Qt
::
AlignLeft
;
}
case
Qt
::
DisplayRole
:
{
switch
(
index
.
column
())
{
case
0
:
return
matchInfo
.
game_id
();
case
1
:
return
QString
::
fromStdString
(
matchInfo
.
game_name
());
case
2
:
{
QStringList
playerList
;
for
(
int
i
=
0
;
i
<
matchInfo
.
player_names_size
();
++
i
)
playerList
.
append
(
QString
::
fromStdString
(
matchInfo
.
player_names
(
i
)));
return
playerList
.
join
(
", "
);
}
case
4
:
return
QDateTime
::
fromTime_t
(
matchInfo
.
time_started
());
case
5
:
return
matchInfo
.
length
();
default:
return
QVariant
();
}
}
case
Qt
::
DecorationRole
:
switch
(
index
.
column
())
{
case
0
:
return
dirIcon
;
case
3
:
return
matchInfo
.
do_not_hide
()
?
lockIcon
:
QVariant
();
default:
return
QVariant
();
}
}
}
return
QVariant
();
}
QVariant
RemoteReplayList_TreeModel
::
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
)
const
{
if
(
orientation
!=
Qt
::
Horizontal
)
return
QVariant
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
switch
(
section
)
{
case
0
:
case
5
:
return
Qt
::
AlignRight
;
default:
return
Qt
::
AlignLeft
;
}
case
Qt
::
DisplayRole
:
{
switch
(
section
)
{
case
0
:
return
tr
(
"ID"
);
case
1
:
return
tr
(
"Name"
);
case
2
:
return
tr
(
"Players"
);
case
3
:
return
tr
(
"Keep"
);
case
4
:
return
tr
(
"Time started"
);
case
5
:
return
tr
(
"Duration (sec)"
);
default:
return
QVariant
();
}
}
default:
return
QVariant
();
}
if
(
orientation
!=
Qt
::
Horizontal
)
return
QVariant
();
switch
(
role
)
{
case
Qt
::
TextAlignmentRole
:
switch
(
section
)
{
case
0
:
case
5
:
return
Qt
::
AlignRight
;
default:
return
Qt
::
AlignLeft
;
}
case
Qt
::
DisplayRole
:
{
switch
(
section
)
{
case
0
:
return
tr
(
"ID"
);
case
1
:
return
tr
(
"Name"
);
case
2
:
return
tr
(
"Players"
);
case
3
:
return
tr
(
"Keep"
);
case
4
:
return
tr
(
"Time started"
);
case
5
:
return
tr
(
"Duration (sec)"
);
default:
return
QVariant
();
}
}
default:
return
QVariant
();
}
}
QModelIndex
RemoteReplayList_TreeModel
::
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
)
const
{
if
(
!
hasIndex
(
row
,
column
,
parent
))
return
QModelIndex
();
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
parent
.
internalPointer
()));
if
(
matchNode
)
{
if
(
row
>=
matchNode
->
size
())
return
QModelIndex
();
return
createIndex
(
row
,
column
,
(
void
*
)
matchNode
->
at
(
row
));
}
else
{
if
(
row
>=
replayMatches
.
size
())
return
QModelIndex
();
return
createIndex
(
row
,
column
,
(
void
*
)
replayMatches
[
row
]);
}
if
(
!
hasIndex
(
row
,
column
,
parent
))
return
QModelIndex
();
MatchNode
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
parent
.
internalPointer
()));
if
(
matchNode
)
{
if
(
row
>=
matchNode
->
size
())
return
QModelIndex
();
return
createIndex
(
row
,
column
,
(
void
*
)
matchNode
->
at
(
row
));
}
else
{
if
(
row
>=
replayMatches
.
size
())
return
QModelIndex
();
return
createIndex
(
row
,
column
,
(
void
*
)
replayMatches
[
row
]);
}
}
QModelIndex
RemoteReplayList_TreeModel
::
parent
(
const
QModelIndex
&
ind
)
const
{
MatchNode
const
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
ind
.
internalPointer
()));
if
(
matchNode
)
return
QModelIndex
();
else
{
ReplayNode
*
replayNode
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
ind
.
internalPointer
()));
return
createIndex
(
replayNode
->
getParent
()
->
indexOf
(
replayNode
),
0
,
replayNode
);
}
MatchNode
const
*
matchNode
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
ind
.
internalPointer
()));
if
(
matchNode
)
return
QModelIndex
();
else
{
ReplayNode
*
replayNode
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
ind
.
internalPointer
()));
return
createIndex
(
replayNode
->
getParent
()
->
indexOf
(
replayNode
),
0
,
replayNode
);
}
}
Qt
::
ItemFlags
RemoteReplayList_TreeModel
::
flags
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
0
;
if
(
!
index
.
isValid
())
return
0
;
return
Qt
::
ItemIsEnabled
|
Qt
::
ItemIsSelectable
;
return
Qt
::
ItemIsEnabled
|
Qt
::
ItemIsSelectable
;
}
ServerInfo_Replay
const
*
RemoteReplayList_TreeModel
::
getReplay
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
0
;
ReplayNode
*
node
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
return
0
;
return
&
node
->
getReplayInfo
();
if
(
!
index
.
isValid
())
return
0
;
ReplayNode
*
node
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
return
0
;
return
&
node
->
getReplayInfo
();
}
ServerInfo_ReplayMatch
const
*
RemoteReplayList_TreeModel
::
getReplayMatch
(
const
QModelIndex
&
index
)
const
{
if
(
!
index
.
isValid
())
return
0
;
MatchNode
*
node
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
{
ReplayNode
*
node
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
return
0
;
return
&
node
->
getParent
()
->
getMatchInfo
();
}
else
return
&
node
->
getMatchInfo
();
if
(
!
index
.
isValid
())
return
0
;
MatchNode
*
node
=
dynamic_cast
<
MatchNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
{
ReplayNode
*
node
=
dynamic_cast
<
ReplayNode
*>
(
static_cast
<
Node
*>
(
index
.
internalPointer
()));
if
(
!
node
)
return
0
;
return
&
node
->
getParent
()
->
getMatchInfo
();
}
else
return
&
node
->
getMatchInfo
();
}
void
RemoteReplayList_TreeModel
::
clearTree
()
{
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
delete
replayMatches
[
i
];
replayMatches
.
clear
();
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
delete
replayMatches
[
i
];
replayMatches
.
clear
();
}
void
RemoteReplayList_TreeModel
::
refreshTree
()
{
PendingCommand
*
pend
=
client
->
prepareSessionCommand
(
Command_ReplayList
());
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
replayListFinished
(
const
Response
&
)));
client
->
sendCommand
(
pend
);
PendingCommand
*
pend
=
client
->
prepareSessionCommand
(
Command_ReplayList
());
connect
(
pend
,
SIGNAL
(
finished
(
Response
,
CommandContainer
,
QVariant
)),
this
,
SLOT
(
replayListFinished
(
const
Response
&
)));
client
->
sendCommand
(
pend
);
}
void
RemoteReplayList_TreeModel
::
addMatchInfo
(
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
beginInsertRows
(
QModelIndex
(),
replayMatches
.
size
(),
replayMatches
.
size
());
replayMatches
.
append
(
new
MatchNode
(
matchInfo
));
endInsertRows
();
emit
treeRefreshed
();
beginInsertRows
(
QModelIndex
(),
replayMatches
.
size
(),
replayMatches
.
size
());
replayMatches
.
append
(
new
MatchNode
(
matchInfo
));
endInsertRows
();
emit
treeRefreshed
();
}
void
RemoteReplayList_TreeModel
::
updateMatchInfo
(
int
gameId
,
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
if
(
replayMatches
[
i
]
->
getMatchInfo
().
game_id
()
==
gameId
)
{
replayMatches
[
i
]
->
updateMatchInfo
(
matchInfo
);
emit
dataChanged
(
createIndex
(
i
,
0
,
(
void
*
)
replayMatches
[
i
]),
createIndex
(
i
,
numberOfColumns
-
1
,
(
void
*
)
replayMatches
[
i
]));
break
;
}
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
if
(
replayMatches
[
i
]
->
getMatchInfo
().
game_id
()
==
gameId
)
{
replayMatches
[
i
]
->
updateMatchInfo
(
matchInfo
);
emit
dataChanged
(
createIndex
(
i
,
0
,
(
void
*
)
replayMatches
[
i
]),
createIndex
(
i
,
numberOfColumns
-
1
,
(
void
*
)
replayMatches
[
i
]));
break
;
}
}
void
RemoteReplayList_TreeModel
::
removeMatchInfo
(
int
gameId
)
{
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
if
(
replayMatches
[
i
]
->
getMatchInfo
().
game_id
()
==
gameId
)
{
beginRemoveRows
(
QModelIndex
(),
i
,
i
);
replayMatches
.
removeAt
(
i
);
endRemoveRows
();
break
;
}
for
(
int
i
=
0
;
i
<
replayMatches
.
size
();
++
i
)
if
(
replayMatches
[
i
]
->
getMatchInfo
().
game_id
()
==
gameId
)
{
beginRemoveRows
(
QModelIndex
(),
i
,
i
);
replayMatches
.
removeAt
(
i
);
endRemoveRows
();
break
;
}
}
void
RemoteReplayList_TreeModel
::
replayListFinished
(
const
Response
&
r
)
{
const
Response_ReplayList
&
resp
=
r
.
GetExtension
(
Response_ReplayList
::
ext
);
beginResetModel
();
clearTree
();
for
(
int
i
=
0
;
i
<
resp
.
match_list_size
();
++
i
)
replayMatches
.
append
(
new
MatchNode
(
resp
.
match_list
(
i
)));
endResetModel
();
emit
treeRefreshed
();
const
Response_ReplayList
&
resp
=
r
.
GetExtension
(
Response_ReplayList
::
ext
);
beginResetModel
();
clearTree
();
for
(
int
i
=
0
;
i
<
resp
.
match_list_size
();
++
i
)
replayMatches
.
append
(
new
MatchNode
(
resp
.
match_list
(
i
)));
endResetModel
();
emit
treeRefreshed
();
}
RemoteReplayList_TreeWidget
::
RemoteReplayList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
)
:
QTreeView
(
parent
)
:
QTreeView
(
parent
)
{
treeModel
=
new
RemoteReplayList_TreeModel
(
_client
,
this
);
proxyModel
=
new
QSortFilterProxyModel
(
this
);
proxyModel
->
setSourceModel
(
treeModel
);
proxyModel
->
setDynamicSortFilter
(
true
);
proxyModel
->
setSortCaseSensitivity
(
Qt
::
CaseInsensitive
);
setModel
(
proxyModel
);
treeModel
=
new
RemoteReplayList_TreeModel
(
_client
,
this
);
proxyModel
=
new
QSortFilterProxyModel
(
this
);
proxyModel
->
setSourceModel
(
treeModel
);
proxyModel
->
setDynamicSortFilter
(
true
);
proxyModel
->
setSortCaseSensitivity
(
Qt
::
CaseInsensitive
);
setModel
(
proxyModel
);
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
header
()
->
setStretchLastSection
(
false
);
setUniformRowHeights
(
true
);
setSortingEnabled
(
true
);
proxyModel
->
sort
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setSortIndicator
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setResizeMode
(
QHeaderView
::
ResizeToContents
);
header
()
->
setStretchLastSection
(
false
);
setUniformRowHeights
(
true
);
setSortingEnabled
(
true
);
proxyModel
->
sort
(
0
,
Qt
::
AscendingOrder
);
header
()
->
setSortIndicator
(
0
,
Qt
::
AscendingOrder
);
}
ServerInfo_Replay
const
*
RemoteReplayList_TreeWidget
::
getCurrentReplay
()
const
{
return
treeModel
->
getReplay
(
proxyModel
->
mapToSource
(
selectionModel
()
->
currentIndex
()));
return
treeModel
->
getReplay
(
proxyModel
->
mapToSource
(
selectionModel
()
->
currentIndex
()));
}
ServerInfo_ReplayMatch
const
*
RemoteReplayList_TreeWidget
::
getCurrentReplayMatch
()
const
{
return
treeModel
->
getReplayMatch
(
proxyModel
->
mapToSource
(
selectionModel
()
->
currentIndex
()));
return
treeModel
->
getReplayMatch
(
proxyModel
->
mapToSource
(
selectionModel
()
->
currentIndex
()));
}
cockatrice/src/remotereplaylist_treewidget.h
View file @
1bc48a78
...
...
@@ -12,82 +12,82 @@ class AbstractClient;
class
QSortFilterProxyModel
;
class
RemoteReplayList_TreeModel
:
public
QAbstractItemModel
{
Q_OBJECT
Q_OBJECT
private:
class
MatchNode
;
class
ReplayNode
;
class
Node
{
protected:
QString
name
;
public:
Node
(
const
QString
&
_name
)
:
name
(
_name
)
{
}
virtual
~
Node
()
{
};
QString
getName
()
const
{
return
name
;
}
};
class
MatchNode
:
public
Node
,
public
QList
<
ReplayNode
*>
{
private:
ServerInfo_ReplayMatch
matchInfo
;
public:
MatchNode
(
const
ServerInfo_ReplayMatch
&
_matchInfo
);
~
MatchNode
();
void
clearTree
();
const
ServerInfo_ReplayMatch
&
getMatchInfo
()
{
return
matchInfo
;
}
void
updateMatchInfo
(
const
ServerInfo_ReplayMatch
&
_matchInfo
);
};
class
ReplayNode
:
public
Node
{
private:
MatchNode
*
parent
;
ServerInfo_Replay
replayInfo
;
public:
ReplayNode
(
const
ServerInfo_Replay
&
_replayInfo
,
MatchNode
*
_parent
)
:
Node
(
QString
::
fromStdString
(
_replayInfo
.
replay_name
())),
parent
(
_parent
),
replayInfo
(
_replayInfo
)
{
}
MatchNode
*
getParent
()
const
{
return
parent
;
}
const
ServerInfo_Replay
&
getReplayInfo
()
{
return
replayInfo
;
}
};
AbstractClient
*
client
;
QList
<
MatchNode
*>
replayMatches
;
QIcon
dirIcon
,
fileIcon
,
lockIcon
;
void
clearTree
();
static
const
int
numberOfColumns
;
class
MatchNode
;
class
ReplayNode
;
class
Node
{
protected:
QString
name
;
public:
Node
(
const
QString
&
_name
)
:
name
(
_name
)
{
}
virtual
~
Node
()
{
};
QString
getName
()
const
{
return
name
;
}
};
class
MatchNode
:
public
Node
,
public
QList
<
ReplayNode
*>
{
private:
ServerInfo_ReplayMatch
matchInfo
;
public:
MatchNode
(
const
ServerInfo_ReplayMatch
&
_matchInfo
);
~
MatchNode
();
void
clearTree
();
const
ServerInfo_ReplayMatch
&
getMatchInfo
()
{
return
matchInfo
;
}
void
updateMatchInfo
(
const
ServerInfo_ReplayMatch
&
_matchInfo
);
};
class
ReplayNode
:
public
Node
{
private:
MatchNode
*
parent
;
ServerInfo_Replay
replayInfo
;
public:
ReplayNode
(
const
ServerInfo_Replay
&
_replayInfo
,
MatchNode
*
_parent
)
:
Node
(
QString
::
fromStdString
(
_replayInfo
.
replay_name
())),
parent
(
_parent
),
replayInfo
(
_replayInfo
)
{
}
MatchNode
*
getParent
()
const
{
return
parent
;
}
const
ServerInfo_Replay
&
getReplayInfo
()
{
return
replayInfo
;
}
};
AbstractClient
*
client
;
QList
<
MatchNode
*>
replayMatches
;
QIcon
dirIcon
,
fileIcon
,
lockIcon
;
void
clearTree
();
static
const
int
numberOfColumns
;
signals:
void
treeRefreshed
();
void
treeRefreshed
();
private
slots
:
void
replayListFinished
(
const
Response
&
r
);
void
replayListFinished
(
const
Response
&
r
);
public:
RemoteReplayList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
=
0
);
~
RemoteReplayList_TreeModel
();
int
rowCount
(
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
{
return
numberOfColumns
;
}
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
QModelIndex
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
QModelIndex
parent
(
const
QModelIndex
&
index
)
const
;
Qt
::
ItemFlags
flags
(
const
QModelIndex
&
index
)
const
;
void
refreshTree
();
ServerInfo_Replay
const
*
getReplay
(
const
QModelIndex
&
index
)
const
;
ServerInfo_ReplayMatch
const
*
getReplayMatch
(
const
QModelIndex
&
index
)
const
;
void
addMatchInfo
(
const
ServerInfo_ReplayMatch
&
matchInfo
);
void
updateMatchInfo
(
int
gameId
,
const
ServerInfo_ReplayMatch
&
matchInfo
);
void
removeMatchInfo
(
int
gameId
);
RemoteReplayList_TreeModel
(
AbstractClient
*
_client
,
QObject
*
parent
=
0
);
~
RemoteReplayList_TreeModel
();
int
rowCount
(
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
{
return
numberOfColumns
;
}
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
QModelIndex
index
(
int
row
,
int
column
,
const
QModelIndex
&
parent
=
QModelIndex
())
const
;
QModelIndex
parent
(
const
QModelIndex
&
index
)
const
;
Qt
::
ItemFlags
flags
(
const
QModelIndex
&
index
)
const
;
void
refreshTree
();
ServerInfo_Replay
const
*
getReplay
(
const
QModelIndex
&
index
)
const
;
ServerInfo_ReplayMatch
const
*
getReplayMatch
(
const
QModelIndex
&
index
)
const
;
void
addMatchInfo
(
const
ServerInfo_ReplayMatch
&
matchInfo
);
void
updateMatchInfo
(
int
gameId
,
const
ServerInfo_ReplayMatch
&
matchInfo
);
void
removeMatchInfo
(
int
gameId
);
};
class
RemoteReplayList_TreeWidget
:
public
QTreeView
{
private:
RemoteReplayList_TreeModel
*
treeModel
;
QSortFilterProxyModel
*
proxyModel
;
ServerInfo_Replay
const
*
getNode
(
const
QModelIndex
&
ind
)
const
;
RemoteReplayList_TreeModel
*
treeModel
;
QSortFilterProxyModel
*
proxyModel
;
ServerInfo_Replay
const
*
getNode
(
const
QModelIndex
&
ind
)
const
;
public:
RemoteReplayList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
=
0
);
ServerInfo_Replay
const
*
getCurrentReplay
()
const
;
ServerInfo_ReplayMatch
const
*
getCurrentReplayMatch
()
const
;
void
refreshTree
();
void
addMatchInfo
(
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
treeModel
->
addMatchInfo
(
matchInfo
);
}
void
updateMatchInfo
(
int
gameId
,
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
treeModel
->
updateMatchInfo
(
gameId
,
matchInfo
);
}
void
removeMatchInfo
(
int
gameId
)
{
treeModel
->
removeMatchInfo
(
gameId
);
}
RemoteReplayList_TreeWidget
(
AbstractClient
*
_client
,
QWidget
*
parent
=
0
);
ServerInfo_Replay
const
*
getCurrentReplay
()
const
;
ServerInfo_ReplayMatch
const
*
getCurrentReplayMatch
()
const
;
void
refreshTree
();
void
addMatchInfo
(
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
treeModel
->
addMatchInfo
(
matchInfo
);
}
void
updateMatchInfo
(
int
gameId
,
const
ServerInfo_ReplayMatch
&
matchInfo
)
{
treeModel
->
updateMatchInfo
(
gameId
,
matchInfo
);
}
void
removeMatchInfo
(
int
gameId
)
{
treeModel
->
removeMatchInfo
(
gameId
);
}
};
#endif
cockatrice/src/replay_timeline_widget.cpp
View file @
1bc48a78
...
...
@@ -5,97 +5,97 @@
#include
<math.h>
ReplayTimelineWidget
::
ReplayTimelineWidget
(
QWidget
*
parent
)
:
QWidget
(
parent
),
maxBinValue
(
1
),
maxTime
(
1
),
timeScaleFactor
(
1.0
),
currentTime
(
0
),
currentEvent
(
0
)
:
QWidget
(
parent
),
maxBinValue
(
1
),
maxTime
(
1
),
timeScaleFactor
(
1.0
),
currentTime
(
0
),
currentEvent
(
0
)
{
replayTimer
=
new
QTimer
(
this
);
connect
(
replayTimer
,
SIGNAL
(
timeout
()),
this
,
SLOT
(
replayTimerTimeout
()));
replayTimer
=
new
QTimer
(
this
);
connect
(
replayTimer
,
SIGNAL
(
timeout
()),
this
,
SLOT
(
replayTimerTimeout
()));
}
const
int
ReplayTimelineWidget
::
binLength
=
5000
;
void
ReplayTimelineWidget
::
setTimeline
(
const
QList
<
int
>
&
_replayTimeline
)
{
replayTimeline
=
_replayTimeline
;
histogram
.
clear
();
int
binEndTime
=
binLength
-
1
;
int
binValue
=
0
;
for
(
int
i
=
0
;
i
<
replayTimeline
.
size
();
++
i
)
{
if
(
replayTimeline
[
i
]
>
binEndTime
)
{
histogram
.
append
(
binValue
);
if
(
binValue
>
maxBinValue
)
maxBinValue
=
binValue
;
while
(
replayTimeline
[
i
]
>
binEndTime
+
binLength
)
{
histogram
.
append
(
0
);
binEndTime
+=
binLength
;
}
binValue
=
1
;
binEndTime
+=
binLength
;
}
else
++
binValue
;
}
histogram
.
append
(
binValue
);
if
(
!
replayTimeline
.
isEmpty
())
maxTime
=
replayTimeline
.
last
();
update
();
replayTimeline
=
_replayTimeline
;
histogram
.
clear
();
int
binEndTime
=
binLength
-
1
;
int
binValue
=
0
;
for
(
int
i
=
0
;
i
<
replayTimeline
.
size
();
++
i
)
{
if
(
replayTimeline
[
i
]
>
binEndTime
)
{
histogram
.
append
(
binValue
);
if
(
binValue
>
maxBinValue
)
maxBinValue
=
binValue
;
while
(
replayTimeline
[
i
]
>
binEndTime
+
binLength
)
{
histogram
.
append
(
0
);
binEndTime
+=
binLength
;
}
binValue
=
1
;
binEndTime
+=
binLength
;
}
else
++
binValue
;
}
histogram
.
append
(
binValue
);
if
(
!
replayTimeline
.
isEmpty
())
maxTime
=
replayTimeline
.
last
();
update
();
}
void
ReplayTimelineWidget
::
paintEvent
(
QPaintEvent
*
event
)
{
QPainter
painter
(
this
);
painter
.
drawRect
(
0
,
0
,
width
()
-
1
,
height
()
-
1
);
qreal
binWidth
=
(
qreal
)
width
()
/
histogram
.
size
();
QPainterPath
path
;
path
.
moveTo
(
0
,
height
()
-
1
);
for
(
int
i
=
0
;
i
<
histogram
.
size
();
++
i
)
path
.
lineTo
(
round
(
i
*
binWidth
),
(
height
()
-
1
)
*
(
1.0
-
(
qreal
)
histogram
[
i
]
/
maxBinValue
));
path
.
lineTo
(
width
()
-
1
,
height
()
-
1
);
path
.
lineTo
(
0
,
height
()
-
1
);
painter
.
fillPath
(
path
,
Qt
::
black
);
const
QColor
barColor
=
QColor
::
fromHsv
(
120
,
255
,
255
,
100
);
painter
.
fillRect
(
0
,
0
,
(
width
()
-
1
)
*
currentTime
/
maxTime
,
height
()
-
1
,
barColor
);
QPainter
painter
(
this
);
painter
.
drawRect
(
0
,
0
,
width
()
-
1
,
height
()
-
1
);
qreal
binWidth
=
(
qreal
)
width
()
/
histogram
.
size
();
QPainterPath
path
;
path
.
moveTo
(
0
,
height
()
-
1
);
for
(
int
i
=
0
;
i
<
histogram
.
size
();
++
i
)
path
.
lineTo
(
round
(
i
*
binWidth
),
(
height
()
-
1
)
*
(
1.0
-
(
qreal
)
histogram
[
i
]
/
maxBinValue
));
path
.
lineTo
(
width
()
-
1
,
height
()
-
1
);
path
.
lineTo
(
0
,
height
()
-
1
);
painter
.
fillPath
(
path
,
Qt
::
black
);
const
QColor
barColor
=
QColor
::
fromHsv
(
120
,
255
,
255
,
100
);
painter
.
fillRect
(
0
,
0
,
(
width
()
-
1
)
*
currentTime
/
maxTime
,
height
()
-
1
,
barColor
);
}
QSize
ReplayTimelineWidget
::
sizeHint
()
const
{
return
QSize
(
-
1
,
50
);
return
QSize
(
-
1
,
50
);
}
QSize
ReplayTimelineWidget
::
minimumSizeHint
()
const
{
return
QSize
(
400
,
50
);
return
QSize
(
400
,
50
);
}
void
ReplayTimelineWidget
::
replayTimerTimeout
()
{
currentTime
+=
200
;
while
((
currentEvent
<
replayTimeline
.
size
())
&&
(
replayTimeline
[
currentEvent
]
<
currentTime
))
{
emit
processNextEvent
();
++
currentEvent
;
}
if
(
currentEvent
==
replayTimeline
.
size
())
{
emit
replayFinished
();
replayTimer
->
stop
();
}
if
(
!
(
currentTime
%
1000
))
update
();
currentTime
+=
200
;
while
((
currentEvent
<
replayTimeline
.
size
())
&&
(
replayTimeline
[
currentEvent
]
<
currentTime
))
{
emit
processNextEvent
();
++
currentEvent
;
}
if
(
currentEvent
==
replayTimeline
.
size
())
{
emit
replayFinished
();
replayTimer
->
stop
();
}
if
(
!
(
currentTime
%
1000
))
update
();
}
void
ReplayTimelineWidget
::
setTimeScaleFactor
(
qreal
_timeScaleFactor
)
{
timeScaleFactor
=
_timeScaleFactor
;
replayTimer
->
setInterval
(
200
/
timeScaleFactor
);
timeScaleFactor
=
_timeScaleFactor
;
replayTimer
->
setInterval
(
200
/
timeScaleFactor
);
}
void
ReplayTimelineWidget
::
startReplay
()
{
replayTimer
->
start
(
200
/
timeScaleFactor
);
replayTimer
->
start
(
200
/
timeScaleFactor
);
}
void
ReplayTimelineWidget
::
stopReplay
()
{
replayTimer
->
stop
();
replayTimer
->
stop
();
}
cockatrice/src/replay_timeline_widget.h
View file @
1bc48a78
...
...
@@ -8,33 +8,33 @@ class QPaintEvent;
class
QTimer
;
class
ReplayTimelineWidget
:
public
QWidget
{
Q_OBJECT
Q_OBJECT
signals:
void
processNextEvent
();
void
replayFinished
();
void
processNextEvent
();
void
replayFinished
();
private:
QTimer
*
replayTimer
;
QList
<
int
>
replayTimeline
;
QList
<
int
>
histogram
;
static
const
int
binLength
;
int
maxBinValue
,
maxTime
;
qreal
timeScaleFactor
;
int
currentTime
;
int
currentEvent
;
QTimer
*
replayTimer
;
QList
<
int
>
replayTimeline
;
QList
<
int
>
histogram
;
static
const
int
binLength
;
int
maxBinValue
,
maxTime
;
qreal
timeScaleFactor
;
int
currentTime
;
int
currentEvent
;
private
slots
:
void
replayTimerTimeout
();
void
replayTimerTimeout
();
public:
ReplayTimelineWidget
(
QWidget
*
parent
=
0
);
void
setTimeline
(
const
QList
<
int
>
&
_replayTimeline
);
QSize
sizeHint
()
const
;
QSize
minimumSizeHint
()
const
;
void
setTimeScaleFactor
(
qreal
_timeScaleFactor
);
int
getCurrentEvent
()
const
{
return
currentEvent
;
}
ReplayTimelineWidget
(
QWidget
*
parent
=
0
);
void
setTimeline
(
const
QList
<
int
>
&
_replayTimeline
);
QSize
sizeHint
()
const
;
QSize
minimumSizeHint
()
const
;
void
setTimeScaleFactor
(
qreal
_timeScaleFactor
);
int
getCurrentEvent
()
const
{
return
currentEvent
;
}
public
slots
:
void
startReplay
();
void
stopReplay
();
void
startReplay
();
void
stopReplay
();
protected:
void
paintEvent
(
QPaintEvent
*
event
);
void
paintEvent
(
QPaintEvent
*
event
);
};
#endif
cockatrice/src/selectzone.cpp
View file @
1bc48a78
...
...
@@ -5,52 +5,52 @@
#include
<QDebug>
SelectZone
::
SelectZone
(
Player
*
_player
,
const
QString
&
_name
,
bool
_hasCardAttr
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
,
bool
isView
)
:
CardZone
(
_player
,
_name
,
_hasCardAttr
,
_isShufflable
,
_contentsKnown
,
parent
,
isView
)
:
CardZone
(
_player
,
_name
,
_hasCardAttr
,
_isShufflable
,
_contentsKnown
,
parent
,
isView
)
{
}
void
SelectZone
::
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
)
{
if
(
event
->
buttons
().
testFlag
(
Qt
::
LeftButton
))
{
QPointF
pos
=
event
->
pos
();
if
(
pos
.
x
()
<
0
)
pos
.
setX
(
0
);
QRectF
br
=
boundingRect
();
if
(
pos
.
x
()
>
br
.
width
())
pos
.
setX
(
br
.
width
());
if
(
pos
.
y
()
<
0
)
pos
.
setY
(
0
);
if
(
pos
.
y
()
>
br
.
height
())
pos
.
setY
(
br
.
height
());
QRectF
selectionRect
=
QRectF
(
selectionOrigin
,
pos
).
normalized
();
for
(
int
i
=
0
;
i
<
cards
.
size
();
++
i
)
{
if
(
cards
[
i
]
->
getAttachedTo
())
if
(
cards
[
i
]
->
getAttachedTo
()
->
getZone
()
!=
this
)
continue
;
cards
[
i
]
->
setSelected
(
selectionRect
.
intersects
(
cards
[
i
]
->
mapRectToParent
(
cards
[
i
]
->
boundingRect
())));
}
static_cast
<
GameScene
*>
(
scene
())
->
resizeRubberBand
(
deviceTransform
(
static_cast
<
GameScene
*>
(
scene
())
->
getViewportTransform
()).
map
(
pos
));
event
->
accept
();
}
if
(
event
->
buttons
().
testFlag
(
Qt
::
LeftButton
))
{
QPointF
pos
=
event
->
pos
();
if
(
pos
.
x
()
<
0
)
pos
.
setX
(
0
);
QRectF
br
=
boundingRect
();
if
(
pos
.
x
()
>
br
.
width
())
pos
.
setX
(
br
.
width
());
if
(
pos
.
y
()
<
0
)
pos
.
setY
(
0
);
if
(
pos
.
y
()
>
br
.
height
())
pos
.
setY
(
br
.
height
());
QRectF
selectionRect
=
QRectF
(
selectionOrigin
,
pos
).
normalized
();
for
(
int
i
=
0
;
i
<
cards
.
size
();
++
i
)
{
if
(
cards
[
i
]
->
getAttachedTo
())
if
(
cards
[
i
]
->
getAttachedTo
()
->
getZone
()
!=
this
)
continue
;
cards
[
i
]
->
setSelected
(
selectionRect
.
intersects
(
cards
[
i
]
->
mapRectToParent
(
cards
[
i
]
->
boundingRect
())));
}
static_cast
<
GameScene
*>
(
scene
())
->
resizeRubberBand
(
deviceTransform
(
static_cast
<
GameScene
*>
(
scene
())
->
getViewportTransform
()).
map
(
pos
));
event
->
accept
();
}
}
void
SelectZone
::
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
)
{
if
(
event
->
button
()
==
Qt
::
LeftButton
)
{
scene
()
->
clearSelection
();
selectionOrigin
=
event
->
pos
();
static_cast
<
GameScene
*>
(
scene
())
->
startRubberBand
(
event
->
scenePos
());
event
->
accept
();
}
else
CardZone
::
mousePressEvent
(
event
);
if
(
event
->
button
()
==
Qt
::
LeftButton
)
{
scene
()
->
clearSelection
();
selectionOrigin
=
event
->
pos
();
static_cast
<
GameScene
*>
(
scene
())
->
startRubberBand
(
event
->
scenePos
());
event
->
accept
();
}
else
CardZone
::
mousePressEvent
(
event
);
}
void
SelectZone
::
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*
event
)
{
selectionOrigin
=
QPoint
();
static_cast
<
GameScene
*>
(
scene
())
->
stopRubberBand
();
event
->
accept
();
selectionOrigin
=
QPoint
();
static_cast
<
GameScene
*>
(
scene
())
->
stopRubberBand
();
event
->
accept
();
}
cockatrice/src/selectzone.h
View file @
1bc48a78
...
...
@@ -4,15 +4,15 @@
#include
"cardzone.h"
class
SelectZone
:
public
CardZone
{
Q_OBJECT
Q_OBJECT
private:
QPointF
selectionOrigin
;
QPointF
selectionOrigin
;
protected:
void
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseMoveEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mousePressEvent
(
QGraphicsSceneMouseEvent
*
event
);
void
mouseReleaseEvent
(
QGraphicsSceneMouseEvent
*
event
);
public:
SelectZone
(
Player
*
_player
,
const
QString
&
_name
,
bool
_hasCardAttr
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
=
0
,
bool
isView
=
false
);
SelectZone
(
Player
*
_player
,
const
QString
&
_name
,
bool
_hasCardAttr
,
bool
_isShufflable
,
bool
_contentsKnown
,
QGraphicsItem
*
parent
=
0
,
bool
isView
=
false
);
};
#endif
Prev
1
2
3
4
5
6
7
8
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment